How do I find out which directories are listed in my system’s PYTHONPATH
variable, from within a Python script (or the interactive shell)?
![](../../users/profiles/10794031.webp)
- 5,829
- 13
- 26
- 41
![](../../users/profiles/20578.webp)
- 89,393
- 53
- 186
- 261
-
25I'm not sure what are you trying to do, but if you want to know which folders are used to search for modules being imported you should not rely on `PYTHONPATH`. Use `sys.path` for that. – Vanuan Nov 16 '12 at 15:02
-
By simple experiment, I found Vanuan's answer below (printing sys.path) just prints PYTHONPATH. This works after we alter the value using add_path(new_path) which adds to PYTHONPATH. – Chan Kim May 30 '16 at 10:20
-
6The title of this post and the body ask two different questions. `sys.path` is "A list of strings that specifies the search path for modules" - https://docs.python.org/2/library/sys.html#sys.path. PYTHONPATH is an environment variable that effects this list. By any reasonable definition `sys.path` is your "python path". – semisecure Jun 01 '16 at 02:07
-
7Don't forget about `python -m site`. – Andrew McKinlay Jul 02 '18 at 15:15
9 Answers
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
.
![](../../users/profiles/99024.webp)
- 25,939
- 9
- 90
- 96
-
Same. Linux aaa.com 2.6.18-4-686-bigmem #1 SMP Wed Jun 6 09:41:07 UTC 2007 i686 GNU/Linux ... Debian Sarge – Spechal Nov 16 '12 at 08:34
-
8This is the platform- and environment- independent way to get the current runtime's python path. – Dmitry Minkovsky Nov 29 '12 at 19:06
-
15This answer was much more helpful for my situation than the accepted answer. I know what my environment variables is. I needed to know where python decided to point besides my environment variable. – Vorticity Apr 17 '13 at 19:55
-
1For Python 3: ```python -c "import sys; print('\n'.join(sys.path))"``` – mirceamironenco Jul 01 '17 at 14:12
-
-
I find it easier to use the following, since it makes it clear if the empty string (`''`) is in the path: `python -c "import sys, pprint; pprint.pprint(sys.path)"` ( And I found this answer more helpful, too; the title of the question mislead me into thinking it was about the actual path python was using, rather than the contents of the `PYTHONPATH` environment variable.) – cjs Nov 01 '17 at 04:06
-
I used `sys.path[0]` to get the "deffault" path where a local file ist stored. Oterwise I get an array with lots of paths. e.g `fiel=open(MyLogfile.log, 'w')` – Cutton Eye Jan 25 '18 at 10:43
-
-
-
@juggler Nope, "for" loops are not cleaner than functional style. It's a matter of preference and programming paradigm. – Vanuan Aug 17 '20 at 18:57
-
@Vanuan -I meant that the output was cleaner. from what I could tell.. maybe the output from your one-liner is also as clean? you're right that it's a matter of preference :-) peace :-) – juggler Aug 19 '20 at 00:07
-
@mirceamironenco's solution can be adapted to support Linux/Unix path appending via: `export PYTHONPATH="python -c \"import sys; print(':'.join(sys.path))\""` – David Golembiowski Oct 28 '20 at 18:48
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 = []
![](../../users/profiles/5987.webp)
- 271,357
- 39
- 345
- 578
-
3(or, more generically ...split(os.sep) ). Can't figure out why you're not getting the love, Mark. Your reply is technically more accurate than Paul D Waite's own reply to his question ???? – mjv Sep 28 '09 at 22:46
-
Ah, excellent. Thanks guys, I suspected I was doing something wrong. I’ve deleted my wrong answer; Mark, if you could edit your answer to include the os.sep bit, the points will rightfully be yours. – Paul D. Waite Sep 30 '09 at 11:50
-
5os.sep is incorrect, see http://stackoverflow.com/questions/1499019/how-to-get-the-path-separator-in-python – Mark Ransom Sep 30 '09 at 16:03
-
2And that problem with the separator is probably why I wasn't getting the love. Thanks for setting me straight. – Mark Ransom Sep 30 '09 at 16:03
-
12And if receive a KeyError, does it means that PYTHONPATH is not defined in my system? Is that a problem? Thanks – glarrain Sep 13 '11 at 19:56
-
9@glarrin, correct - KeyError means that PYTHONPATH is not defined. It won't be a problem because there's already default paths set up, see `sys.path`. – Mark Ransom Sep 13 '11 at 20:03
-
I'm not seeing an answer here to solve the `PYTHONPATH` `KeyError` error. Do I need to define `PYTHONPATH` in relation to `sys.path`? – MikeiLL Jul 08 '14 at 00:16
-
@MikeiLL you're right, I've edited the answer to include that. If there's no `PYTHONPATH` then the user didn't specify one and an empty list is the proper result. If you're more interested in the list that includes the defaults use `sys.path` instead. – Mark Ransom Jul 08 '14 at 00:43
-
Thanks, Mark. You might even want to add `print user_paths` to the code. Also I found this link to be enlightening as far as PYTHONPATH: http://www.stereoplex.com/blog/understanding-imports-and-pythonpath – MikeiLL Jul 08 '14 at 16:26
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.sep 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['PYTHONPATH'].split(os.pathsep)
![](../../users/profiles/180697.webp)
- 2,950
- 1
- 22
- 22
-
6For future readers: `os.sep` returns the directory separator for the operating system, e.g. `/`. The separator used in the Python path is different, and returned by `os.pathsep` as shown in the accepted answer. – Paul D. Waite May 22 '10 at 10:35
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.
![](../../users/profiles/9749972.webp)
- 335
- 2
- 6
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)
![](../../users/profiles/2203705.webp)
- 239
- 1
- 3
- 15
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:\Users\martin\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
>>>
![](../../users/profiles/13439784.webp)
- 51
- 1
import subprocess
python_path = subprocess.check_output("which python", shell=True).strip()
python_path = python_path.decode('utf-8')
![](../../users/profiles/9605125.webp)
- 71
- 4
import sys
for a in sys.path:
a.replace('\\\\','\\')
print(a)
It will give all the paths ready for place in the Windows.
![](../../users/profiles/8928024.webp)
- 3,318
- 8
- 26
- 39
![](../../users/profiles/14148129.webp)
- 3
- 1