How do I find out which directories are listed in my system’s PYTHONPATH
variable, from within a Python script (or the interactive shell)?
views:
2672answers:
2
+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
2009-09-28 22:08:36
(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
2009-09-28 22:46:36
mjv - sometimes voting here can make little sense
foosion
2009-09-28 23:41:26
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
2009-09-30 11:50:01
os.sep is incorrect, see http://stackoverflow.com/questions/1499019/how-to-get-the-path-separator-in-python
Mark Ransom
2009-09-30 16:03:06
And that problem with the separator is probably why I wasn't getting the love. Thanks for setting me straight.
Mark Ransom
2009-09-30 16:03:39
Bravo. Iteration: it works.
Paul D. Waite
2009-09-30 16:47:50
+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
2009-09-28 22:27:31
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
2010-05-22 10:35:39