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)
Related
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 6 years ago.
I have macOS Sierra, using python 2.7 for a homework.
I have an import problem as followed :
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/networkx/release.py", line 43, in <module>
import time
File "/Users/chen/Desktop/minesparis/JE/Chronomap/scripts/time.py", line 3, in <module>
import matplotlib.pyplot as plt
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 122, in <module>
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook.py", line 33, in <module>
import numpy as np
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 201, in <module>
from . import random
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/random/__init__.py", line 99, in <module>
from .mtrand import *
File "mtrand.pyx", line 151, in init mtrand (numpy/random/mtrand/mtrand.c:37668)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dummy_threading.py", line 45, in <module>
import threading
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 15, in <module>
from time import time as _time, sleep as _sleep
ImportError: cannot import name time
I open the script threading.py and try to run it and everything seems working.
When I write
from time import time as _time, sleep as _sleep
on my console, the import works.
so I don't know what is the problem and how can I solve it..
Thank you very much!
Mia
You named your script time.py. It relies on the builtin module time.
Now, if you were the Python Interpreter, how would you tell the difference between your time script and the builtin one? It can't! Instead, local files take precedence.
Simply rename your script to something else.
I just started with programming, I wanted to import the urllib resource but when I just use this code:
import urllib2
It worked 2 months ago (took a break).
It gives me this error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1157, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 10, in <module>
import string, re
File "/home/younes/Python/string.py", line 11
for total:
^
I used Python 2 for that, also when using Python 3 it doesn't find the module. (it also doesn't find the urllib3 module?)
I have reinstalled Python but it didn't work.
I am using Ubuntu 14.04.
The file /home/younes/Python/string.py is preventing import of the standard library module string which in turn prevent import of modules depending on the string module. In addition to that, the file has an syntax error.
Remove the file or rename the file. If there's /home/younes/Python/string.pyc, also remove/rename it.
You have a file called string.py which shadows the builtin string module. Also, consider starting with a newer Python 3 instead of the old Python 2.
if use python3 , from urllib import requests or import urllib.request and usage:
urllib.request.urlopen("http:xxx")
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).
When I try importing urllib2 , i get the following error
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 79, in <module>
import mimetools
File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/lib/python2.7/tempfile.py", line 34, in <module>
from random import Random as _Random
ImportError: cannot import name Random
i know that there is no module called Random, but i did check urllib2.py and there was code which imported Random.
I am using Python 2.7
Maybe you have a random.py file or module on your pythonpath so python search in it to find random.Random (that exists on python2.7).
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.