How do I find out my PYTHONPATH using Python?
Question:
How do I find out which directories are listed in my system’s PYTHONPATH
variable, from within a Python script (or the interactive shell)?
Answers:
sys.path
might include items that aren’t specifically in your PYTHONPATH
environment variable. To query the variable directly, use:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
Can’t seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.pathsep as below:
sys.path might include items that aren’t specifically in your PYTHONPATH environment variable. To query the variable directly, use:
import os
os.environ.get('PYTHONPATH', '').split(os.pathsep)
You would probably also want this:
import sys
print(sys.path)
Or as a one liner from the terminal:
python -c "import sys; print('n'.join(sys.path))"
Caveat: If you have multiple versions of Python installed you should use a corresponding command python2
or python3
.
PYTHONPATH is an environment variable whose value is a list of directories. Once set, it is used by Python to search for imported modules, along with other std. and 3rd-party library directories listed in Python’s “sys.path”.
As any other environment variables, you can either export it in shell or in ~/.bashrc, see here.
You can query os.environ[‘PYTHONPATH’] for its value in Python as shown below:
$ python3 -c "import os, sys; print(os.environ['PYTHONPATH']); print(sys.path) if 'PYTHONPATH' in sorted(os.environ) else print('PYTHONPATH is not defined')"
IF defined in shell as
$ export PYTHONPATH=$HOME/Documents/DjangoTutorial/mysite
THEN result =>
/home/Documents/DjangoTutorial/mysite
['', '/home/Documents/DjangoTutorial/mysite', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
ELSE result =>
PYTHONPATH is not defined
To set PYTHONPATH to multiple paths, see here.
Note that one can add or delete a search path via sys.path.insert(), del or remove() at run-time, but NOT through os.environ[].
Example:
>>> os.environ['PYTHONPATH']="$HOME/Documents/DjangoTutorial/mysite"
>>> 'PYTHONPATH' in sorted(os.environ)
True
>>> sys.path // but Not there
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>> sys.path.insert(0,os.environ['PYTHONPATH'])
>>> sys.path // It's there
['$HOME/Documents/DjangoTutorial/mysite', '', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>>
In summary, PYTHONPATH is one way of specifying the Python search path(s) for imported modules in sys.path. You can also apply list operations directly to sys.path without the aid of PYTHONPATH.
Python tells me where it lives when it gives me an error message 🙂
>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:UsersmartinAppDataLocalProgramsPythonPython36-32libos.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
>>>
If using conda, you can get the env prefix using os.environ["CONDA_PREFIX"]
.
Works in windows 10, essentially identical to vanuan’s answer, but cleaner (taken from somewhere, can’t remember where..):
import sys
for p in sys.path:
print(p)
import sys
for a in sys.path:
a = a.replace('\\','\')
print(a)
It will give all the paths ready for place in the Windows.
import subprocess
python_path = subprocess.check_output("which python", shell=True).strip()
python_path = python_path.decode('utf-8')
Use the command,
$ which python
remember to enter this in the correct environment so use:
$ conda activate <env>
or
$ mamba activate <env>
If you do not have a conda environment, $ which python
or $ which python3
would do just fine.
How do I find out which directories are listed in my system’s PYTHONPATH
variable, from within a Python script (or the interactive shell)?
sys.path
might include items that aren’t specifically in your PYTHONPATH
environment variable. To query the variable directly, use:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
Can’t seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.pathsep as below:
sys.path might include items that aren’t specifically in your PYTHONPATH environment variable. To query the variable directly, use:
import os
os.environ.get('PYTHONPATH', '').split(os.pathsep)
You would probably also want this:
import sys
print(sys.path)
Or as a one liner from the terminal:
python -c "import sys; print('n'.join(sys.path))"
Caveat: If you have multiple versions of Python installed you should use a corresponding command python2
or python3
.
PYTHONPATH is an environment variable whose value is a list of directories. Once set, it is used by Python to search for imported modules, along with other std. and 3rd-party library directories listed in Python’s “sys.path”.
As any other environment variables, you can either export it in shell or in ~/.bashrc, see here.
You can query os.environ[‘PYTHONPATH’] for its value in Python as shown below:
$ python3 -c "import os, sys; print(os.environ['PYTHONPATH']); print(sys.path) if 'PYTHONPATH' in sorted(os.environ) else print('PYTHONPATH is not defined')"
IF defined in shell as
$ export PYTHONPATH=$HOME/Documents/DjangoTutorial/mysite
THEN result =>
/home/Documents/DjangoTutorial/mysite
['', '/home/Documents/DjangoTutorial/mysite', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
ELSE result =>
PYTHONPATH is not defined
To set PYTHONPATH to multiple paths, see here.
Note that one can add or delete a search path via sys.path.insert(), del or remove() at run-time, but NOT through os.environ[].
Example:
>>> os.environ['PYTHONPATH']="$HOME/Documents/DjangoTutorial/mysite"
>>> 'PYTHONPATH' in sorted(os.environ)
True
>>> sys.path // but Not there
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>> sys.path.insert(0,os.environ['PYTHONPATH'])
>>> sys.path // It's there
['$HOME/Documents/DjangoTutorial/mysite', '', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>>
In summary, PYTHONPATH is one way of specifying the Python search path(s) for imported modules in sys.path. You can also apply list operations directly to sys.path without the aid of PYTHONPATH.
Python tells me where it lives when it gives me an error message 🙂
>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:UsersmartinAppDataLocalProgramsPythonPython36-32libos.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
>>>
If using conda, you can get the env prefix using os.environ["CONDA_PREFIX"]
.
Works in windows 10, essentially identical to vanuan’s answer, but cleaner (taken from somewhere, can’t remember where..):
import sys
for p in sys.path:
print(p)
import sys
for a in sys.path:
a = a.replace('\\','\')
print(a)
It will give all the paths ready for place in the Windows.
import subprocess
python_path = subprocess.check_output("which python", shell=True).strip()
python_path = python_path.decode('utf-8')
Use the command,
$ which python
remember to enter this in the correct environment so use:
$ conda activate <env>
or
$ mamba activate <env>
If you do not have a conda environment, $ which python
or $ which python3
would do just fine.