Twisted 是否改变了它的依赖关系?

发布于 2024-08-30 16:39:02 字数 1567 浏览 4 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2024-09-06 16:39:02

您自己的文件之一 /home/cdecker/dev/acn/acn_a4/src/operator.py 隐藏了 Python 的内置 operator 模块。您应该将自己的 operator.py 重命名为其他名称。

您可以在此处看到问题:

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>

Twisted 尝试导入运算符,但 Python 会加载您自己的模块之一。

为了防止将来出现类似的情况,您可能不应该将 src 文件夹添加到 PYTHONPATH 中。相反,创建一个包,以便您自己的文件显示为 myproject.mymodule 并且不能隐藏内置文件。

One of your own files, /home/cdecker/dev/acn/acn_a4/src/operator.py shadows Python's builtin operator module. You should rename your own operator.py to something else.

You can see the problem here:

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>

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.

挖鼻大婶 2024-09-06 16:39:02

当由于模块或包或名称不存在而无法导入名称时,在 import 语句中引发 ImportError 。在您的情况下,operator 模块中不存在 attrgetter

第一个想法是在项目的主目录中定义一个名为 operator 的模块。模块或包按照 sys.path 顺序进行搜索,如果您在主目录中定义了具有相同名称的模块,则会隐藏搜索路径中具有相同名称的所有其他模块。

ImportError is raised on import statement when a name cannot be imported, because module or package or name doesn't exists. In your case attrgetter doesn't exists in operator module.

The first idea is that you define a module called operator in the project's main directory. Modules, or packages, are searched following sys.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文