How do I find out my PYTHONPATH using Python?
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 = []
How to get the PYTHONPATH in shell?
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
How can I find where Python is installed on Windows?
In your Python interpreter, type the following commands:
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
how to print contents of PYTHONPATH
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
Find full path of the Python interpreter?
sys.executable
contains full path of the currently running Python interpreter.
import sys
print(sys.executable)
which is now documented here
How to retrieve a module's path?
import a_module
print(a_module.__file__)
Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)
You can also try:
path = os.path.dirname(a_module.__file__)
To get the module's directory.
How do I find the location of my Python site-packages directory?
There are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in
sys.path
when you run:python -m site
For a more concise list run
getsitepackages
from the site module in Python code:python -c 'import site; print(site.getsitepackages())'
Caution: In virtual environments getsitepackages is not available with older versions of
virtualenv
,sys.path
from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
The per user site-packages directory (PEP 370) is where Python installs your local packages:
python -m site --user-site
If this points to a non-existing directory check the exit status of Python and see
python -m site --help
for explanations.Hint: Running
pip list --user
orpip freeze --user
gives you a list of all installed per user site-packages.
Practical Tips
<package>.__path__
lets you identify the location(s) of a specific package: (details)$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']<module>.__file__
lets you identify the location of a specific module: (difference)$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.pyRun
pip show <package>
to show Debian-style package information:$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
I can't locate my python path (but able to locate python3 path)
What version of MacOS are you running? Starting in 12.3, Python2 was removed from the system.
https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes
Python
Deprecations
Python 2.7 was removed from macOS in this update. Developers should use Python 3 or an alternative language instead. (39795874)
Can't find my PYTHONPATH
Python does some stuff up front when it is started, probably also setting that path in windows. Just set it and see, if it is changed in sys.path
.
Setting environment variables in the Python docs say:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables
Related Topics
Why Is Pip Installing an Old Version of My Package
Find the Indexes of All Regex Matches
How to Convert a Scikit-Learn Dataset to a Pandas Dataset
Using Psycopg2 with Lambda to Update Redshift (Python)
How to Check If One Dictionary Is a Subset of Another Larger Dictionary
Monkey Patching a Class in Another Module in Python
How to Draw a Line with Matplotlib
Python Parsing Bracketed Blocks
Safest Way to Convert Float to Integer in Python
Good or Bad Practice in Python: Import in the Middle of a File
What Does a Leading '\X' Mean in a Python String '\Xaa'
Remove or Replace Spaces in Column Names
What Is the Correct Syntax for 'Else If'
Efficient Way to Remove Keys with Empty Strings from a Dict
How Does Python Find a Module File If the Import Statement Only Contains the Filename