views:

93

answers:

2

Hi all,

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

+8  A: 

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.

THC4k
Works perfectly, and wow, that was a quick answer :-)
cdecker
+1  A: 

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.

mg