Twisted 是否改变了它的依赖关系?
我目前正在开发一个 Python/Twisted 项目,该项目将在 Planetlab 上分发和测试。由于某种原因,我的代码在周五可以正常工作,现在我想测试一个小的更改,但它根本无法工作:
Traceback (most recent call last):
File "acn_a4/src/node.py", line 6, in <module>
from twisted.internet.protocol import DatagramProtocol
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/__init__.py", line 18, in <module>
from twisted.python import compat
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/compat.py", line 146, in <module>
import operator
File "/home/cdecker/dev/acn/acn_a4/src/operator.py", line 7, in <module>
File "/home/cdecker/acn_a4/src/node.py", line 6, in <module>
from twisted.internet.protocol import DatagramProtocol
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/internet/protocol.py", line 20, in <module>
from twisted.python import log, failure, components
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/log.py", line 19, in <module>
from twisted.python import util, context, reflect
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/util.py", line 5, in <module>
import os, sys, hmac, errno, new, inspect, warnings
File "/usr/lib/python2.5/inspect.py", line 32, in <module>
from operator import attrgetter
ImportError: cannot import name attrgetter
而且由于我对 python 还很陌生,所以我不知道是什么导致了这个问题。
欢迎所有建议:-)
I'm currently working on a Python/Twisted project which is to be distributed and tested on Planetlab. For some reason my code was working on friday and now that I wanted to test a minor change it refuses to work at all:
Traceback (most recent call last):
File "acn_a4/src/node.py", line 6, in <module>
from twisted.internet.protocol import DatagramProtocol
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/__init__.py", line 18, in <module>
from twisted.python import compat
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/compat.py", line 146, in <module>
import operator
File "/home/cdecker/dev/acn/acn_a4/src/operator.py", line 7, in <module>
File "/home/cdecker/acn_a4/src/node.py", line 6, in <module>
from twisted.internet.protocol import DatagramProtocol
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/internet/protocol.py", line 20, in <module>
from twisted.python import log, failure, components
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/log.py", line 19, in <module>
from twisted.python import util, context, reflect
File "/usr/lib/python2.5/site-packages/Twisted-10.0.0-py2.5-linux-i686.egg/twisted/python/util.py", line 5, in <module>
import os, sys, hmac, errno, new, inspect, warnings
File "/usr/lib/python2.5/inspect.py", line 32, in <module>
from operator import attrgetter
ImportError: cannot import name attrgetter
And since I'm pretty new to python I have no idea what could have caused this problem.
All suggestions are welcome :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您自己的文件之一
/home/cdecker/dev/acn/acn_a4/src/operator.py
隐藏了 Python 的内置operator
模块。您应该将自己的operator.py
重命名为其他名称。您可以在此处看到问题:
Twisted 尝试
导入运算符
,但 Python 会加载您自己的模块之一。为了防止将来出现类似的情况,您可能不应该将 src 文件夹添加到 PYTHONPATH 中。相反,创建一个包,以便您自己的文件显示为
myproject.mymodule
并且不能隐藏内置文件。One of your own files,
/home/cdecker/dev/acn/acn_a4/src/operator.py
shadows Python's builtinoperator
module. You should rename your ownoperator.py
to something else.You can see the problem here:
Twisted tries to
import operator
but Python loads one of your own modules.To prevent stuff like that in the future you should probably not add your src folder to the PYTHONPATH like that. Create a package instead, so that your own files appear as
myproject.mymodule
and can't shadow builtins.当由于模块或包或名称不存在而无法导入名称时,在
import
语句中引发ImportError
。在您的情况下,operator
模块中不存在attrgetter
。第一个想法是在项目的主目录中定义一个名为
operator
的模块。模块或包按照sys.path
顺序进行搜索,如果您在主目录中定义了具有相同名称的模块,则会隐藏搜索路径中具有相同名称的所有其他模块。ImportError
is raised onimport
statement when a name cannot be imported, because module or package or name doesn't exists. In your caseattrgetter
doesn't exists inoperator
module.The first idea is that you define a module called
operator
in the project's main directory. Modules, or packages, are searched followingsys.path
order, if you define a module with the same name in your main directory, you are hiding all others module with the same name in the search path.