Skip to content Skip to sidebar Skip to footer

Using Python Venv From Script

After creating the virtual environment, If you have a shell script which calls: /home/user/venv/python3 How does it know where the virtualenv's site-packages fo

Solution 1:

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.

Solution 2:

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.

Post a Comment for "Using Python Venv From Script"