1

After creating the virtual environment, If you have a shell script which calls:

/home/user/venv/python3 <scriptname>

How does it know where the virtualenv's site-packages folder is without source activating into the virtualenv (thus changing the path)?

0

2 Answers 2

6

This magic happens with sys.prefix.

Note: If a virtual environment is in effect, this value will be changed in site.py to point to the virtual environment. The value for the Python installation will still be available, via base_prefix.

The site module is imported (from system path!) at interpreter startup, and the site-packages dirs are appended to sys.path with the sys.prefix.

You can verify this for yourself by executing the python REPL with the -S flag to disable importing the site module. You'll find that packages installed in the virtualenv are no longer visible by import statements (assuming they aren't already installed in system site-packages).

Your next question is probably "But how does site itself know if we're in a venv or not?" and the answer is heuristic:

A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment.

If a file named "pyvenv.cfg" exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory. Implemented here.

Sign up to request clarification or add additional context in comments.

Comments

3

Python looks through the values in sys.path for site-packages and these values are automatically set when you execute python3 or python by the site package. Which, is imported during initialization (unless suppressed via the -S flag)

You can refer to the site package documentation for more details about how exactly this is done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.