Has Twisted changed its dependencies?

给你一囗甜甜゛ 提交于 2019-12-10 21:28:37

问题


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 :-)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2753552/has-twisted-changed-its-dependencies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!