60
debian@debian:~$ echo $PYTHONPATH  
/home/qiime/lib/:  
debian@debian:~$ python  
Python 2.7.3 (default, Jan  2 2013, 16:53:07)   
[GCC 4.7.2] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
>>> import sys  
>>> sys.path  
['', '/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg',   
'/usr/local/lib/python2.7/dist-packages/stripogram-1.5-py2.7.egg', '/home/qiime/lib', 
'/home/debian', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',   
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-
dynload',   '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10',  
'/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']    

How can I get all of PYTHONPATH output in bash?
Why echo $PYTHONPATH can not get all of them?

Guy Avraham
  • 2,830
  • 2
  • 31
  • 43
showkey
  • 449
  • 30
  • 101
  • 235
  • 9
    `sys.path` is not `PYTHONPATH`, `sys.path` actually consists of multiple things : current dir,PYTHONPATH,standard library, and paths contained in .pth files if any. http://docs.python.org/2/tutorial/modules.html#the-module-search-path – Ashwini Chaudhary Apr 29 '13 at 01:17

6 Answers6

100

The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:

python -c "import sys; print(sys.path)"

Or if want the output in the UNIX directory list style (separated by :) you can do this:

python -c "import sys; print(':'.join(x for x in sys.path if x))"

Which will output something like this:

/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/
python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us
r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib
/python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist-
packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u
sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:
/usr/lib/pymodules/python2.7
Chris_Rands
  • 30,797
  • 12
  • 66
  • 100
Hubro
  • 48,322
  • 60
  • 196
  • 344
  • Is sys path same as PYTHONPATH? – variable Oct 17 '19 at 11:35
  • @variable No, the paths in `PYTHONPATH` is added to the paths in `sys.path` when the Python interpreter starts. In other words, `sys.path` will include all the paths in `PYTHONPATH`, but also additional paths, like the path to the Python standard library and the path to installed packages. – Hubro Oct 17 '19 at 12:22
  • OK, Please can you advise how I can get the value of PYTHONPATH? – variable Oct 17 '19 at 12:23
  • 1
    @variable In bash `echo $PYTHONPATH`, in Python 3 `import os; print(os.environ["PYTHONPATH"])` – Hubro Oct 17 '19 at 12:24
  • This gives me syntax error (pointing to end of import word - EOL while scanning string literal): python -c 'import os; print(os.environ["PYTHONPATH"])'. If I use double quote then it says "name 'PYTHONPATH' is not defined" – variable Oct 17 '19 at 12:26
  • @variable This works for me (Python 3): `PYTHONPATH=test python -c 'import os; print(os.environ["PYTHONPATH"])'`. If you're still having problems I'm in the [Python 3 chat room](https://chat.stackoverflow.com/rooms/6/python). Let's not pollute the comment section here :) – Hubro Oct 17 '19 at 12:30
  • slight mod to the original: PYTHONPATH=$(python -c "import os, sys; print(os.pathsep.join(x for x in sys.path if x))") – Neil McGill Feb 02 '20 at 00:04
13

Just write:

just write which python in your terminal and you will see the python path you are using.

Community
  • 1
  • 1
cjahangir
  • 1,503
  • 13
  • 20
  • 24
    That's the path to the python executable NOT the PYTHONPATH. PYTHONPATH is where python itself looks for modules to import. – abalter Aug 02 '16 at 20:01
  • The path where Python itself exists is called PYTHON_HOME or PYTHONHOME – qurban Dec 21 '19 at 21:46
7

Those of us using Python 3.x should do this:

python -c "import sys; print(sys.path)"
zzzzzzz
  • 97
  • 1
  • 3
6

Python, at startup, loads a bunch of values into sys.path (which is "implemented" via a list of strings), including:

  • various hardcoded places
  • the value of $PYTHONPATH
  • probably some stuff from startup files (I'm not sure if Python has rcfiles)

$PYTHONPATH is only one part of the eventual value of sys.path.

If you're after the value of sys.path, the best way would be to ask Python (thanks @Codemonkey):

python -c "import sys; print sys.path"
Guy Avraham
  • 2,830
  • 2
  • 31
  • 43
3

You can also try this:

Python 2.x:
python -c "import sys; print '\n'.join(sys.path)"

Python 3.x:
python3 -c "import sys; print('\n'.join(sys.path))"

The output will be more readable and clean, like so:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /Library/Python/2.7/site-packages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

Community
  • 1
  • 1
  • Please notice that there is a typo in the second line of code. It should read 'python', not 'python3'. – Pauli Jan 08 '20 at 13:09
1

Adding to @zzzzzzz answer, I ran the command:python3 -c "import sys; print(sys.path)" and it provided me with different paths comparing to the same command with python. The paths that were displayed with python3 were "python3 oriented".

See the output of the two different commands:

python -c "import sys; print(sys.path)"

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/setuptools-39.1.0-py2.7.egg', '/usr/lib/python2.7/dist-packages']

python3 -c "import sys; print(sys.path)"

['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

Both commands were executed on my Ubuntu 18.04 machine.

Guy Avraham
  • 2,830
  • 2
  • 31
  • 43