views:

2672

answers:

2

How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)?

+6  A: 

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)
Mark Ransom
(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
mjv - sometimes voting here can make little sense
foosion
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
os.sep is incorrect, see http://stackoverflow.com/questions/1499019/how-to-get-the-path-separator-in-python
Mark Ransom
And that problem with the separator is probably why I wasn't getting the love. Thanks for setting me straight.
Mark Ransom
Bravo. Iteration: it works.
Paul D. Waite
+7  A: 

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
import sys
os.environ['PYTHONPATH'].split(os.sep)
Vitali
For 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