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 :-)
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.
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.
Related
I have a python (2.7) project containing my own packages util and operator (and so forth).
I read about relative imports, but perhaps I didn't understand. I have the following directory structure:
top-dir/
util/__init__.py (empty)
util/ua.py
util/ub.py
operator/__init__.py
...
test/test1.py
The test1.py file contains
#!/usr/bin/env python2
from __future__ import absolute_import # removing this line dosn't change anything. It's default functionality in python2.7 I guess
import numpy as np
It's fine when I execute test1.py inside the test/ folder. But when I move to the top-dir/ the import numpy wants to include my own util package:
Traceback (most recent call last):
File "tests/laplace_2d_square.py", line 4, in <module>
import numpy as np
File "/usr/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/usr/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/usr/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/usr/lib/python2.7/site-packages/numpy/core/__init__.py", line 45, in <module>
from numpy.testing import Tester
File "/usr/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
from unittest import TestCase
File "/usr/lib/python2.7/unittest/__init__.py", line 58, in <module>
from .result import TestResult
File "/usr/lib/python2.7/unittest/result.py", line 9, in <module>
from . import util
File "/usr/lib/python2.7/unittest/util.py", line 2, in <module>
from collections import namedtuple, OrderedDict
File "/usr/lib/python2.7/collections.py", line 9, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name itemgetter
The troublesome line is either
from . import util
or perhaps
from operator import itemgetter as _itemgetter, eq as _eq
What can I do?
operator is a module within the Python standard library. Giving a module of yours the same name as a standard module calls for trouble, and should be avoided.
If you absolutely need a way to get around this, you could try playing with the sys.path variable. The first element is usually the directory of the script, or the empty string which directs the import system to the current directory.
oldpath = sys.path.pop(0)
import numpy
sys.path.insert(0, oldpath)
I know this question has been asked many times, but I couldn't manage to solve it on my own. So please bear with me.
Traceback (most recent call last):
File "/Users/someone/Desktop/eclipse/plugins/org.python.pydev_2.6.0.2012062818/pysrc/pydevd.py", line 3, in <module>
import pydev_imports
File "/Users/someone/Desktop/eclipse/plugins/org.python.pydev_2.6.0.2012062818/pysrc/pydev_imports.py", line 14, in <module>
from _pydev_SimpleXMLRPCServer import SimpleXMLRPCServer
File "/Users/someone/Desktop/eclipse/plugins/org.python.pydev_2.6.0.2012062818/pysrc/_pydev_SimpleXMLRPCServer.py", line 116, in <module>
import BaseHTTPServer
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 81, in <module>
import mimetools
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 34, in <module>
from random import Random as _Random
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
echo $PATH gave me this. /Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
My goal is to setup an IDE that gives me code completion and breakpoint debug(like java, so that I can debug it line by line). I've read many posts here, but still haven't find a solution to it. Can anyone point to the right direction? Thanks in advance.
Edit:
I upgraded python to 2.7.3 because i couldn't select the interpreter in eclipse preferences.
Take a look at this link. All you need to do is make a proper configuration of your interpreter.
If it doesn't help, you should take a look at another one (the second one is about urandom module, whilst the first one about pydev configuration).
I'm fairly new to Python, and I don't understand what is going on here.
C:\Users\Mike\Desktop>storytext testGui.py
Traceback (most recent call last):
File "C:\Python25\Scripts\storytext.py", line 19, in <module>
from storytext.cmdline import main
File "C:\Python25\lib\site-packages\storytext-3.7-py2.5.egg\storytext\cmdline.py", line 3, in <module>
import scriptengine, definitions
File "C:\Python25\lib\site-packages\storytext-3.7-py2.5.egg\storytext\scriptengine.py", line 4, in
<module>
import recorder, replayer
File "C:\Python25\lib\site-packages\storytext-3.7-py2.5.egg\storytext\recorder.py", line 6, in <module>
from replayer import ReplayScript
File "C:\Python25\lib\site-packages\storytext-3.7-py2.5.egg\storytext\replayer.py", line 5, in <module>
from threading import Thread, Timer
File "C:\Python25\lib\threading.py", line 13, in <module>
from collections import deque
ImportError: No module named collections
Ok, no Module named collections. Indeed, when I search my python25 folder for collections, it's not there. I've read that it's located at Modules/_collectionsmodule.c, which I also can't find.
The kicker to the whole thing is that I can import collections from the interpreter, and I can from threading import Time, Timer in the interpreter.
I do have both python 2.5 and 2.6 on this machine, and it's running Windows 7.
Can anyone point me in the right direction?
I never know how to read these. It would be helpful if someone could help me understand this one, and maybe give advice on how to read these in general?
D:\>python captain2.py
Traceback (most recent call last):
File "captain2.py", line 2, in <module>
from twisted.internet import reactor
File "c:\Python27\lib\site-packages\twisted\internet\reactor.py", line 37, in
<module>
from twisted.internet import default
File "c:\Python27\lib\site-packages\twisted\internet\default.py", line 50, in
<module>
install = _getInstallFunction(platform)
File "c:\Python27\lib\site-packages\twisted\internet\default.py", line 46, in
_getInstallFunction
from twisted.internet.selectreactor import install
File "c:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 1
4, in <module>
from zope.interface import implements
ImportError: No module named zope.interface
In a stack traceback, Python lists the current call stack, in the order the calls happened. So, first your code in captain2.py:
from twisted.internet import reactor
Then the reactor module did:
from twisted.internet import default
and so on, until selectreactor.py did:
from zope.interface import implements
and apparently there does not exist a zope.interface module on your system.
In my script, I've imported urrlib2 and the script was working fine. After reboot, I get the following error:
File "demo.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.6/urllib2.py", line 92, in <module>
import httplib
File "/usr/lib/python2.6/httplib.py", line 78, in <module>
import mimetools
File "/usr/lib/python2.6/mimetools.py", line 6, in <module>
import tempfile
File "/usr/lib/python2.6/tempfile.py", line 34, in <module>
from random import Random as _Random
ImportError: cannot import name Random
And when I do import random separately, it works fine. Any ideas what might be wrong?
I'm using ubuntu 9.10 (up to date). thanks
The usual answer is that you've got a file called random.py in the current directory when the script is running. tempfile would be accidentally importing that random and not the stdlib random module.
Check that random is the stdlib's module and not some arbitrary module with the same name from sys.path.
>>> inspect.getabsfile(random)