Checking if running inside a virtual environment
suggest changeSometimes the shell prompt doesn’t display the name of the virtual environment and you want to be sure if you are in a virtual environment or not.
Run the python interpreter and try:
import sys
sys.prefix
sys.real_prefix
- Outside a virtual, environment
sys.prefix
will point to the system python installation andsys.real_prefix
is not defined. - Inside a virtual environment,
sys.prefix
will point to the virtual environment python installation andsys.real_prefix
will point to the system python installation.
For virtual environments created using the standard library venv module there is no sys.real_prefix
. Instead, check whether sys.base_prefix
is the same as sys.prefix
.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents