Search code examples
pythonpython-venv

Python venv: Dependence on default OS version


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?


Solution

  • 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
    #