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).
Related
This question already has answers here:
How can I import from the standard library, when my project has a module with the same name? (How can I control where Python looks for modules?)
(6 answers)
Closed 2 years ago.
I just installed urllib3 using pip command and was successful, and when i use import urllib3 or import urllib its showing a huge error. Why this happening?. Please help.
This is my error: when use import urllib or import urllib3 and running in sublime text.
Traceback (most recent call last):
File "C:\Users\Toshiba\Desktop\dd.py", line 1, in <module>
import urllib3
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 11, in <module>
from .exceptions import (
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\packages\six.py", line 199, in load_module
mod = mod._resolve()
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\packages\six.py", line 113, in _resolve
return _import_module(self.mod)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\email\parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\email\feedparser.py", line 27, in <module>
from email._policybase import compat32
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\email\_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\email\utils.py", line 31, in <module>
import urllib.parse
File "C:\Users\Toshiba\Desktop\urllib.py", line 2, in <module>
from urllib3 import PoolManager
ImportError: cannot import name 'PoolManager' from partially initialized module 'urllib3' (most likely due to a circular import) (C:\Users\Toshiba\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\__init__.py)
File "C:\Users\Toshiba\Desktop\urllib.py", line 2, in
One of your files is called urllib. Rename it.
That's one of the issues but I'm not sure it's the root cause of your problem. It seems like your installation is broken. If renaming this file didn't help, I'd suggest to start with a new virtualenv. If you're not using one, start with that.
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")
Am facing the below error while trying to import Flask. Similar questions posted had issues with Werkzeug versions
from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request
File "init__.py", line 25, in <module>
from flask import Flask
File "user/anaconda/lib/python2.7/site-packages/flask/__init__.py", line 17,
in <module>
from werkzeug.exceptions import abort
File "user/anaconda/lib/python2.7/site-packages/werkzeug/__init__.py", line
154, in <module>
__import__('werkzeug.exceptions')
File "user/anaconda/lib/python2.7/site-packages/werkzeug/exceptions.py", line
71, in <module>
from werkzeug.wrappers import Response
File "user/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line
26, in <module>
from werkzeug.http import HTTP_STATUS_CODES, \
File "user/anaconda/lib/python2.7/site-packages/werkzeug/http.py", line 28, in
<module>
from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request'
Werkzeug version(I tried with 10.1,10.4 ) still the error remains.
Someone pointed out that there might be a local copy of urllib2.. I
figured out that there was both urllib and urllib2 indeed in my
local path..
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
Now I am not sure how to proceed, Should I uninstall urllib/urllib2 ?
EDIT: The solution listed in Tried to use relative imports, and broke my import paths? doesn't work for me. In this case, I tried importing urllib2 into my Flask project and printing out the file path, but that throws the below exceptions
File "/user/__init__.py", line 25, in <module>
import urllib2
File "/user/anaconda/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/user/anaconda/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/user/anaconda/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/user/anaconda/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/user/anaconda/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/user/anaconda/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyErr_ReplaceException
Referenced from: /user/anaconda/lib/python2.7/lib-dynload/_io.so
Expected in: dynamic lookup
I had a similar issue, and I followed the recommendation in the solution by adding from urllib2 import parse_http_list as _parse_list_header before from flask import Flask, then I got this error message:
"/Users/anaconda/lib/python2.7/io.py", line 51, in import _io
ImportError: dlopen(/Users/anaconda/lib/python2.7/lib-dynload/_io.so, 2):
Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /Users/anaconda/lib/python2.7/lib-dynload/_io.so
Expected in: dynamic lookup
which I then resolved by following the solution:
sudo find / -name _io.so
I replaced /Users/anaconda/lib/python2.7/lib-dynload/_io.so with /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
It works for me!
In my case, one of my python-modules named uu. Such as a file in python3-email module.
... line 4, in <module>
import flask
File "/usr/lib/python3/dist-packages/werkzeug/http.py", line 28, in <module>
from urllib.request import parse_http_list as _parse_list_header
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 10, in <module>
import uu
Then, i have the same error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/werkzeug/http.py", line 26, in <module>
from urllib2 import parse_http_list as _parse_list_header
ImportError: No module named 'urllib2'
I hope, this example will help you.
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)