skip to Main Content

I would like to have multiple virtual environments, some with a different python version itself. I am uncertain about the base version of the python I should use. For instace, if I create venv dependent virtual environments using python provided by my current Ubuntu OS, say 3.8, and later I upgrade Ubuntu itself and that updates python to version 3.10, will the virtual environments created under previous Ubuntu still work?

3

Answers


  1. The way venvs work is that they bring along a whole Python runtime with them. Indeed if you activate the venv and then run which python you will not see your system Python, but the one installed by the venv.

    In that way, if you upgrade your OS and that also upgrades your system Python, you won’t have to worry because your venv will still have the old version vendored in place.

    Login or Signup to reply.
  2. virtual environment essentially copy from OS installation to the venv, so you will be maintainint all the libraries in the venv folder.

    When you upgrade the OS the binaries will stay there and you will have both version available in different venv

    Login or Signup to reply.
  3. Virtualenvs do depend on their base interpreter being intact.

    If the OS upgrade (or any other process) removes, say, python3.8, then no, venvs whose base interpreter is that python3.8 won’t work anymore.

    To demonstrate this in a Docker container, let’s install Python 3.10 and 3.11, create venvs with both of them, then remove one base interpreter to see that the respective venv no longer works:

    ~ $ docker run -it ubuntu:22.04 bash
    # apt update
    [...snip...]
    # apt install -y python3.10 python3.11 python3.10-venv python3.11-venv
    [...snip...]
    # python3.10 -m venv venv-310
    # python3.11 -m venv venv-311
    # venv-310/bin/python --version
    Python 3.10.12
    # venv-311/bin/python --version
    Python 3.11.0rc1
    # apt purge 'python3.10-*'
    [...snip...]
    # venv-310/bin/python --version
    bash: venv-310/bin/python: No such file or directory
    # venv-311/bin/python --version
    Python 3.11.0rc1
    # 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search