How do I find out my python path 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)?

1
315
6/30/2014 3:27:15 PM

Accepted Answer

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 = []
206
7/8/2014 12:41:43 AM

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))"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon