Behaviour of Python imports while run interpreter by direct path to it

Viewed 27

I run a Python 3.8 interpreter that is in virtual environment created by

python3.8 -m venv ~/my_venv

Important: I don't activate this environment by

source ~/my_venv/bin/activate

Instead I run python interpreter directly, like this:

/home/user/my_venv/bin/python3.8 my_script.py

And in this case I have all virtual environment libraries is included in sys.path, and sys.prefix is set to /home/user/my_venv. Looks like I don't need to explicitly activate environment to use it, just run an interpreter from it's path.

The question is: Is it a standard and documented behavior, or something non-documented and could change in future versions, or even if on different OS installation?


I could find something: https://docs.python.org/3.10/using/unix.html#python-related-paths-and-files

  • python use prefix (sys.prefix) to find libraries directory.

https://docs.python.org/3.10/library/sys.html#sys.prefix

If a virtual environment is in effect, this value will be changed in site.py to point to the virtual environment.

It is also explain something, but it isn't obvious what means "a virtual environment is in effect"? I didn't activated environment usind "source ~/my_venv/bin/activate", I have just run a python interpreter from it's directory. How did it get, that it is in virtual environment, and it needs to change prefix to a specific directory? I found no any "site.py" in the along with python interpreter in this virtual env directory.

Maybe there is something in python documentation, that I missed?

0 Answers
Related