Python 3.5.1 urllib has no attribute request

PythonPython 3.xUrllibUrlopen

Python Problem Overview


I have tried

import urllib.request

or

import urllib

The path for my urllib is /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/__init__.py

I am wondering where is urlopen, or is my python module pointing to the wrong file?

Python Solutions


Solution 1 - Python

According to this, you have to use the following:

import urllib.request

The reason is:

> With packages, like this, you sometimes need to explicitly import the piece you want. That way, the urllib module doesn't have to load everything up just because you wanted one small part.

Solution 2 - Python

Use this way

import urllib.request
urllib.request.urlopen('http://216.58.192.142',timeout=1)

Solution 3 - Python

In My case what i did was that i created a file "http.py" so changing that name to "http_my.py" solved that problem .same as @JohnnyFun

Solution 4 - Python

In python 3.6.x this worked for me, this way I did not have to change the code at all:

import urllib.request as urllib

Solution 5 - Python

If nothing above worked for you, try renaming your python module.

In my particular case, the issue was that the file I was running was called http.py. Once I changed the name to test-http.py, importing urllib.request resolved the error AttributeError: module 'urllib' has no attribute 'request'

I had noticed that further up the exception trace the internal packages were trying to fetch a module called http, so guessing my module's name was wonkin stuff up...

Solution 6 - Python

I had the same issue. I'm using Python 3.8.2.

I checked the _init_.py file located at /usr/lib/python3.8/urllib/ and its empty, which means you can not make a fully qualified access using only import urllib. In this case, you have to use import urllib.request.

Solution 7 - Python

Make sure your title does not have the same name as one of the python modules. i.e, if the name of your file is "socket.py" or "https.py". You will encounter the Attribute Error. Good Luck

Solution 8 - Python

import urllib.request as urllib

then in the code call urlopen function

example:

test = urllib.urlopen("----")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1999806View Question on Stackoverflow
Solution 1 - PythonSwordsmanView Answer on Stackoverflow
Solution 2 - PythonKashifView Answer on Stackoverflow
Solution 3 - PythonArsalan ZabeebView Answer on Stackoverflow
Solution 4 - PythonMPękalskiView Answer on Stackoverflow
Solution 5 - PythonJohnnyFunView Answer on Stackoverflow
Solution 6 - PythonLucas ZamparView Answer on Stackoverflow
Solution 7 - PythonFranck David GnonkaView Answer on Stackoverflow
Solution 8 - PythonHassan SaeedView Answer on Stackoverflow