为什么 sys.path 显示不存在的项目(这会导致导入问题)?
我在 sys.path
中看到一些其他项目,这些项目 1) 不存在,2) 导致导入问题(特别是 Nose
)。
基本上,我创建了一个包(我们称之为 foo
),我在多个项目中使用它。我目前正在处理的项目可以毫无问题地从 foo
导入所有内容,但是当我运行 Nose
时,我收到导入错误:
E
======================================================================
ERROR: Failure: ImportError (No module named foo)
----------------------------------------------------------------------
Traceback (most recent call last):
...
File "/path/to/my-project/file.py", line 6, in <module>
from foo import *
ImportError: No module named foo
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (errors=1)
当我吐出路径时,我得到:
["/path/to/my-project/foo",
"/path/to/my-project/foo",
...,
"/usr/virtualenvs/my-project/lib/python2.6/site-packages/foo-py2.6.egg",
...]
/path/to/my-project/foo
不存在。如果我从 sys.path 中弹出前 2 个条目,一切都会正常。
有人可以向我解释为什么这些项目会出现,而实际上,列表中唯一应该出现的项目是安装到 virtualenv 中的项目?
我该如何阻止这种情况再次发生?与foo
中的setup.py
有关吗?
I'm seeing some additional items in sys.path
which 1) don't exist and 2) cause problems with imports (specifically with Nose
).
Basically, I've created a package (lets call it foo
) which I use in multiple projects. The project I'm working on at the moment can import everything from foo
without issue, but when I run Nose
I get import errors:
E
======================================================================
ERROR: Failure: ImportError (No module named foo)
----------------------------------------------------------------------
Traceback (most recent call last):
...
File "/path/to/my-project/file.py", line 6, in <module>
from foo import *
ImportError: No module named foo
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (errors=1)
When I spit out the path I get:
["/path/to/my-project/foo",
"/path/to/my-project/foo",
...,
"/usr/virtualenvs/my-project/lib/python2.6/site-packages/foo-py2.6.egg",
...]
/path/to/my-project/foo
doesn't exist. If I pop the first 2 entries off sys.path
everything works fine.
Can someone explain to me why those items are showing up when, really, the only one that should be in the list is the one installed into the virtualenv?
And how do I stop this from happening in the future? Is it something to do with the setup.py
in foo
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$PYTHONPATH
中有什么东西吗?即使在 virtualenv 环境中,这也会将条目放入 sys.path 中。尝试在 bash 中取消设置 PYTHONPATH(如果您使用 bash),然后查看您的 sys.path 包含的内容。
Do you have anything in
$PYTHONPATH
? This will put entries in sys.path even within a virtualenv enviroment.Try
unset PYTHONPATH
in bash (if you use bash) and then see what yoursys.path
contains.在路径上的任意位置查找
.pth
文件。这些文件(例如,easy-install.pth)可以包含附加的 sys.path 条目(每行一个)。Look for
.pth
files anywhere on the path. These files (e.g.,easy-install.pth
) can contain additional sys.path entries (one per line).