Troubleshooting Python Sys.path
Solution 1:
I had some issues recently with sys.path and here is how I went about trying to figure out where the entries are coming from. I was able to track all the entries and where they were coming from. Hopefully this will help you too.
The first that is added
C:\WINNT\system32\python27.zip(more details in PEP273).Next ones that are added are from entries in windows registry. The entries
C:\Python27\DLLs;C:\Python27\lib; C:\Python27\lib\plat-win; C:\Python27\lib\lib-tkcome fromHOT_KEY_LOCAL_USER/Python/PythonCore/2.7/PythonPathin the registry. More details in Python source code comments here http://svn.python.org/projects/python/trunk/PC/getpathp.c (These entries were the trickiest for me to understand until I found the link above).Next, as explained in the
sitepackage documentation (link),sys.pathis built fromsys.prefixandsys.exec_prefix. On my computer both of them point toC:\Python27. And by default it searches thelib/site-packagesanywways. So now the entriesC:\Python27; C:\Python27\lib\site-packagesare appended to the list above.Next it searches each of the
.pthfiles in alphabetical order. I haveeasy_install.pth,pywin32.pthandsetuptools.pthin my site-packages. This is where things start getting weird. It would be straightforward if the entries in the.pthfiles were just directory locations. They would just get appended to thesys.pathline by line. Howevereasy_install.pthhas some python code that causes the entries listed ineasy_install.pthto add the packages list at the beginning of thesys.pathlist.After this the directory entries in
pywin32.pth,setuptools.pthare added at the end of thesys.pathlist as expected.
Note: While the above discussion pertains to Windows, it is similar even on Mac etc. On Mac it just adds different OS defaults like darwin etc. before it starts looking at site-packages directory for .pth files.
In your case, you can start by starting a python shell and checking where sys.prefix and sys.exec_prefix point to and then drilling down from there.
Note 2: If you are using an IDE such as Aptana/PyDev it will add more configurations of its own. So you need to be wary of that.
Post a Comment for "Troubleshooting Python Sys.path"