PYTHONPATH 环境变量之前的路径中的鸡蛋 [英] Eggs in path before PYTHONPATH environment variable
问题描述
如果我从 easy_install 安装了软件包,则在 PYTHONPATH
变量中的项目之前,将鸡蛋添加到 sys.path
中.>
例如,如果我在当前目录中安装了一个名为 foo 的 egg 包以及一个名为 foo 的包,然后执行以下操作:
PYTHONPATH="."Python>>>进口富
这将使用 foo
的 egg 版本而不是本地目录.检查 sys.path
显示鸡蛋位于 PYTHONPATH
的之前 项.这似乎坏了.有没有办法覆盖这种行为?
不幸的是,这是通过 setuptools/command/easy_install.py
深处的硬编码模板完成的.您可以使用编辑过的模板创建修补过的 setuptools,但我没有找到从外部扩展 easy_install 的干净方法.
easy_install 每次运行时都会重新生成文件easy_install.pth
.这是一个可以在 easy_install 之后运行的快速脚本,用于从 easy_install.pth
中删除页眉和页脚.您可以创建一个包装 shell 脚本以在 easy_install
之后立即运行它:
#!/usr/bin/env python导入系统路径 = sys.argv[1]行 = 打开(路径,'rb').readlines()如果行和行 [0] 中的导入系统":open(path, 'wb').write(''.join(lines[1:-1]) + '\n')
示例:
% easy_install gdata% PYTHONPATH=xyz python -c 'import sys;打印 sys.path[:2]'['', '/Users/pat/virt/lib/python2.6/site-packages/gdata-2.0.14-py2.6.egg']% ./fix_path ~/virt/lib/python2.6/site-packages/easy_install.pth% PYTHONPATH=xyz python -c 'import sys;打印 sys.path[:2]'['', '/Users/pat/xyz']
为了更清楚,这里是easy-install.pth
的格式:
import sys;sys.__plen = len(sys.path)./gdata-2.0.14-py2.6.egg导入系统;new=sys.path[sys.__plen:];del sys.path[sys.__plen:];p=getattr(sys,'__egginsert',0);sys.path[p:p]=new;sys.__egginsert = p+len(new)
这两行 import sys
是导致鸡蛋出现在路径开头的罪魁祸首.我的脚本只是删除了那些 sys.path
-munging 行.
If I have packages installed from easy_install, the eggs are prepended to sys.path
before the items in the PYTHONPATH
variable.
For example, if I have an egg package called foo installed as well as a package called foo in the current directory, and then do this:
PYTHONPATH="." python
>>> import foo
This will use the egg version of foo
instead of the local directory. Inspecting sys.path
shows that eggs are placed before items from PYTHONPATH
. This seems broken. Is there any way to override this behavior?
Unfortunately this is done with a hard-coded template deep inside setuptools/command/easy_install.py
. You could create a patched setuptools with an edited template, but I've found no clean way to extend easy_install from the outside.
Each time easy_install runs it will regenerate the file easy_install.pth
. Here is a quick script which you can run after easy_install, to remove the header and footer from easy_install.pth
. You could create a wrapper shell script to run this immediately after easy_install
:
#!/usr/bin/env python
import sys
path = sys.argv[1]
lines = open(path, 'rb').readlines()
if lines and 'import sys' in lines[0]:
open(path, 'wb').write(''.join(lines[1:-1]) + '\n')
Example:
% easy_install gdata
% PYTHONPATH=xyz python -c 'import sys; print sys.path[:2]'
['', '/Users/pat/virt/lib/python2.6/site-packages/gdata-2.0.14-py2.6.egg']
% ./fix_path ~/virt/lib/python2.6/site-packages/easy_install.pth
% PYTHONPATH=xyz python -c 'import sys; print sys.path[:2]'
['', '/Users/pat/xyz']
For more clarification, here is the format of easy-install.pth
:
import sys; sys.__plen = len(sys.path)
./gdata-2.0.14-py2.6.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)
The two import sys
lines are the culprit causing the eggs to appear at the start of the path. My script just removes those sys.path
-munging lines.
这篇关于PYTHONPATH 环境变量之前的路径中的鸡蛋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!