简体   繁体   English

python sys.path故障排除

[英]Troubleshooting python sys.path

The python docs at http://docs.python.org/library/sys.html say that sys.path is... http://docs.python.org/library/sys.html上的python文档说sys.path是......

Initialized from the environment variable PYTHONPATH, plus an installation-dependent default. 从环境变量PYTHONPATH初始化,加上依赖于安装的默认值。

I found a path item in my sys.path that was causing problems, and had a lot of trouble tracking it down. 我在我的sys.path中发现了一个导致问题的路径项,并且在跟踪它时遇到了很多麻烦。 All I could turn up on Google was people explaining how to add items to the PYTHONPATH variable. 我可以在Google上看到的是解释如何项目添加PYTHONPATH变量的人。

My question is: are there any tools that can help track down why a particular item is on your sys.path ? 我的问题是:是否有任何工具,可以帮助追查特定项目是在你sys.path How can I find out more about the "installation-dependent default"? 如何找到有关“依赖于安装的默认值”的更多信息?

So far, I've found a partial answer is to use strace on python itself and look for .pth files. 到目前为止,我发现部分答案是在python本身使用strace并查找.pth文件。 I also found a sys.path_importer_cache , which may or may not be applicable. 我还发现了一个sys.path_importer_cache ,它可能适用也可能不适用。

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. 我最近遇到了一些关于sys.path问题,这就是我如何试图弄清楚条目的来源。 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). 第一个添加C:\\WINNT\\system32\\python27.zip python27.zip(PEP273中的更多细节)。

  • Next ones that are added are from entries in windows registry. 接下来添加的是来自Windows注册表中的条目。 The entries C:\\Python27\\DLLs;C:\\Python27\\lib; C:\\Python27\\lib\\plat-win; C:\\Python27\\lib\\lib-tk 条目C:\\Python27\\DLLs;C:\\Python27\\lib; C:\\Python27\\lib\\plat-win; C:\\Python27\\lib\\lib-tk C:\\Python27\\DLLs;C:\\Python27\\lib; C:\\Python27\\lib\\plat-win; C:\\Python27\\lib\\lib-tk C:\\Python27\\DLLs;C:\\Python27\\lib; C:\\Python27\\lib\\plat-win; C:\\Python27\\lib\\lib-tk come from HOT_KEY_LOCAL_USER/Python/PythonCore/2.7/PythonPath in the registry. C:\\Python27\\DLLs;C:\\Python27\\lib; C:\\Python27\\lib\\plat-win; C:\\Python27\\lib\\lib-tk来自HOT_KEY_LOCAL_USER/Python/PythonCore/2.7/PythonPath 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). 有关Python源代码注释的更多详细信息,请访问http://svn.python.org/projects/python/trunk/PC/getpathp.c (这些条目对我来说是最难理解的,直到我找到上面的链接)。

  • Next, as explained in the site package documentation ( link ), sys.path is built from sys.prefix and sys.exec_prefix . 接下来,如site包文档( 链接 )中所述, sys.path是从sys.prefixsys.exec_prefix On my computer both of them point to C:\\Python27 . 在我的计算机上,它们都指向C:\\Python27 And by default it searches the lib/site-packages anywways. 并且默认情况下它会搜索lib/site-packages So now the entries C:\\Python27; C:\\Python27\\lib\\site-packages 所以现在条目C:\\Python27; C:\\Python27\\lib\\site-packages C:\\Python27; C:\\Python27\\lib\\site-packages are appended to the list above. C:\\Python27; C:\\Python27\\lib\\site-packages附加到上面的列表中。

  • Next it searches each of the .pth files in alphabetical order. 接下来,它按字母顺序搜索每个.pth文件。 I have easy_install.pth , pywin32.pth and setuptools.pth in my site-packages. 我的网站包中有easy_install.pthpywin32.pthsetuptools.pth This is where things start getting weird. 事情开始变得怪异。 It would be straightforward if the entries in the .pth files were just directory locations. 如果.pth文件中的条目只是目录位置,那将是直截了当的。 They would just get appended to the sys.path line by line. 它们只会逐行附加到sys.path However easy_install.pth has some python code that causes the entries listed in easy_install.pth to add the packages list at the beginning of the sys.path list. 但是easy_install.pth有一些python代码会导致easy_install.pth列出的条目在sys.path列表的开头添加包列表。

  • After this the directory entries in pywin32.pth , setuptools.pth are added at the end of the sys.path list as expected. 在此之后, pywin32.pthsetuptools.pth中的目录条目pywin32.pth setuptools.pth添加到sys.path列表的末尾。

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. 注意:虽然上面的讨论属于Windows,但它在Mac上也是类似的。在Mac上它只是在开始查看.pth文件的site-packages目录之前添加了不同的操作系统默认值,如darwin等。

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. 在您的情况下,您可以首先启动python shell并检查sys.prefixsys.exec_prefix指向的位置,然后从那里向下钻取。

Note 2: If you are using an IDE such as Aptana/PyDev it will add more configurations of its own. 注意2:如果您使用的是诸如Aptana / PyDev之类的IDE,它将添加更多自己的配置。 So you need to be wary of that. 所以你需要警惕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM