对python sys.path进行故障排除 [英] Troubleshooting python sys.path

查看:164
本文介绍了对python sys.path进行故障排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://docs.python.org/library/sys.html 上的python文档说sys.path是...

The python docs at http://docs.python.org/library/sys.html say that sys.path is...

从环境变量PYTHONPATH初始化,再加上与安装有关的默认值.

Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

我在sys.path中发现了一个路径项目,该路径项目导致了问题,并且在跟踪该项目时遇到了很多麻烦.我只能在Google上看到有人在解释如何将项目添加PYTHONPATH变量中.

I found a path item in my sys.path that was causing problems, and had a lot of trouble tracking it down. All I could turn up on Google was people explaining how to add items to the PYTHONPATH variable.

我的问题是:是否有任何工具可以帮助您查找为什么您的sys.path上的特定项目?如何找到有关依赖于安装的默认设置"的更多信息?

My question is: are there any tools that can help track down why a particular item is on your sys.path? How can I find out more about the "installation-dependent default"?

到目前为止,我发现部分答案是在python本身上使用strace并查找.pth文件.我还找到了sys.path_importer_cache,它可能适用也可能不适用.

So far, I've found a partial answer is to use strace on python itself and look for .pth files. I also found a sys.path_importer_cache, which may or may not be applicable.

推荐答案

我最近在sys.path上遇到了一些问题,这就是我试图找出条目来自何处的方法.我能够跟踪所有条目以及它们来自何处.希望这也会对您有帮助.

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.

  • 第一个添加的内容C:\WINNT\system32\python27.zip(PEP273中有更多详细信息).

  • The first that is added C:\WINNT\system32\python27.zip (more details in PEP273).

接下来要添加的内容来自Windows注册表中的条目.条目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.有关Python源代码的更多详细信息,请参见 http://svn.python.org /projects/python/trunk/PC/getpathp.c (这些条目是我最难理解的,直到找到上面的链接).

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-tk come from HOT_KEY_LOCAL_USER/Python/PythonCore/2.7/PythonPath in 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).

下一步,如site软件包文档中所述(链接 ),sys.pathsys.prefixsys.exec_prefix构建.在我的计算机上,它们都指向C:\Python27.默认情况下,它始终搜索lib/site-packages.因此,条目C:\Python27; C:\Python27\lib\site-packages现在被追加到上面的列表中.

Next, as explained in the site package documentation (link), sys.path is built from sys.prefix and sys.exec_prefix. On my computer both of them point to C:\Python27. And by default it searches the lib/site-packages anywways. So now the entries C:\Python27; C:\Python27\lib\site-packages are appended to the list above.

接下来,它将按字母顺序搜索每个.pth文件.我的站点程序包中有easy_install.pthpywin32.pthsetuptools.pth.这是事情开始变得怪异的地方.如果.pth文件中的条目只是目录位置,那将很简单.它们只会逐行添加到sys.path.但是easy_install.pth有一些python代码,这些代码会导致easy_install.pth中列出的条目将软件包列表添加到sys.path列表的开始处.

Next it searches each of the .pth files in alphabetical order. I have easy_install.pth, pywin32.pth and setuptools.pth in my site-packages. This is where things start getting weird. It would be straightforward if the entries in the .pth files were just directory locations. They would just get appended to the sys.path line by line. 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.

此后,按预期将pywin32.pthsetuptools.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.

注意:尽管以上讨论与Windows有关,但即使在Mac等上也是如此.在Mac上,它仅添加了不同的操作系统默认值(如darwin等),然后才开始查看site-packages目录中的.pth文件.

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.

在您的情况下,您可以先启动python shell,然后检查sys.prefixsys.exec_prefix指向的位置,然后从那里进行钻取.

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.

注2:如果使用的是诸如Aptana/PyDev之类的IDE,它将添加自己的更多配置.因此,您需要对此保持警惕.

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.

这篇关于对python sys.path进行故障排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆