Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

venv.EnvBuilder creates inconsistent venv depending on the name of the Python executable #122033

Open
EtienneMiret opened this issue Jul 19, 2024 · 1 comment
Labels
topic-venv Related to the venv module type-bug An unexpected behavior, bug, or error

Comments

@EtienneMiret
Copy link

EtienneMiret commented Jul 19, 2024

Bug report

Bug description:

Given the below create-venv.py script and several Python 3 installed in the classpath:

import sys
import venv


if __name__ == "__main__":
    venv.EnvBuilder().create(sys.argv[1])

Let’s create various venvs:

for VERSION in 3.9 3.11 3.12; do
  python$VERSION create-venv.py $VERSION-venv
  for EXEC in python python3 python$VERSION; do
    $VERSION-venv/bin/$EXEC create-venv.py $VERSION-$EXEC-venv
  done
done

And now, let’s check them:

for PYTHON in *-venv/bin/python; do
  echo "$PYTHON: $($PYTHON --version)"
done

The above loop outputs:

3.11-python3.11-venv/bin/python: Python 3.11.7
3.11-python3-venv/bin/python: Python 3.9.14
3.11-python-venv/bin/python: Python 3.9.14
3.11-venv/bin/python: Python 3.11.7
3.12-python3.12-venv/bin/python: Python 3.12.1
3.12-python3-venv/bin/python: Python 3.9.14
3.12-python-venv/bin/python: Python 3.9.14
3.12-venv/bin/python: Python 3.12.1
3.9-python3.9-venv/bin/python: Python 3.9.14
3.9-python3-venv/bin/python: Python 3.9.14
3.9-python-venv/bin/python: Python 3.9.14
3.9-venv/bin/python: Python 3.9.14

As the reader can see, Python venv’s created by python or python3 executable of a Python venv will not use the Python of this venv, and instead will use the system’s default Python (3.9 in my case).

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

@FFY00
Copy link
Member

FFY00 commented Jul 22, 2024

This happens because it likely copies/symlinks the interpreters from PATH, which points to the wrong versions as the venv is not active. We could prefer to copy them from the scripts path of the running interpreter, but I am wary of that as some people might be relying on the current behavior.

@picnixz picnixz added the topic-venv Related to the venv module label Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-venv Related to the venv module type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants