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.
python - how to print contents of PYTHONPATH - Stack Overflow
Current Directory not in PYTHONPATH
pathit - Just show me my PATH in way I can read it
python - How to get the PYTHONPATH in shell? - Stack Overflow
How do I add PYTHONPATH inside a Python script?
Is PYTHONPATH required to import installed packages?
How do I set multiple directories in PYTHONPATH?
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.
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 = []
I suggest not to rely on the raw PYTHONPATH because it can vary depending on the OS.
Instead of the PYTHONPATH value in the os.environ dict, use sys.path from the sys module. This is preferrrable, because it is platform independent:
import sys
print(sys.path)
The value of sys.path is initialized from the environment variable PYTHONPATH, plus an installation-dependent default (depending on your OS). For more info see
https://docs.python.org/2/library/sys.html#sys.path
https://docs.python.org/3/library/sys.html#sys.path
If PYTHONPATH hasn't been set then that's expected, maybe default it to an empty string:
import os
print(os.environ.get('PYTHONPATH', ''))
You may also be after:
import sys
print(sys.path)
Got tired of scrolling through my PATH variable and decomposing it with something like echo $PATH | tr ":" "\n", so I wrote micro package to show the path by line, sort it alphabetically and extract lines of interest.
| Question | Answer |
|---|---|
| What's on my PATH? |
pathit show or pathit raw
|
| Sort alphabetically |
pathit show --sort
|
Paths with mingw?
|
pathit show --includes mingw
|
New content PATH without invalid dirs
|
pathit show --purge --string
|
Maybe a better tool exists, but hope it is a useful utility - or at least a demo. Packaging done with `poetry` and few tests added. Code, bash equivalents and install instructions: https://github.com/epogrebnyak/what-the-path
Update: thanks to feedback from this thread, the options in pathit become the following. Note --string in specific - it allows to form a string that you can later use in your shell start file.
--sort --no-sort Sort output alphabetically. --includes TEXT --excludes TEXT --purge --no-purge Exclude invalid directories. --expand --no-expand Expand environment variables if found inside PATH. --string --no-string Print a single string suitable for PATH content. --display-numbers Indicate directory order in PATH. --color Use color to highlight errors.
Update 2: package renamed justpath and available through pip install justpath now. Also can display your PATH as JSON now.
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
Just write:
just write which python in your terminal and you will see the python path you are using.