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)?
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, viabase_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 .
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.