65

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?

mgilson
  • 300,191
  • 65
  • 633
  • 696
user1999806
  • 681
  • 1
  • 5
  • 10
  • Funny. Works for me (and I have the same path). What do you get from `os.listdir(os.path.dirname(urllib.__file__))`? – mgilson May 05 '16 at 04:10
  • That's `['__init__.py', '__pycache__', 'error.py', 'parse.py', 'request.py', 'response.py', 'robotparser.py']` – user1999806 May 05 '16 at 04:24
  • it does sound like your paths are crossed. try `import sys;print(sys.path)` and see if any python 2 related paths appear? if so, perhaps you've got something unusual set in your PYTHONPATH in your shell environment? – ben author May 05 '16 at 04:25
  • 1
    And, for the record, `import urllib.request; urllib.request.urlopen` works fine for me. `import urllib; urllib.request.urlopen` would raise the `AttributeError` that you are seeing. – mgilson May 05 '16 at 04:29
  • Can you successfully execute `from urllib.request import urlopen` ? Are you certain you're executing from Python3? Did you already import `urllib` earlier in the script? Full traceback may be helpful. – sytech May 05 '16 at 04:37
  • `print(sys.path)` shows paths for python 3.5 libraries.. none of python 2 – user1999806 May 05 '16 at 07:07
  • 3
    Somehow this worked guys... `import urllib; from urllib.request import urlopen` which is kind of odd but it worked! – user1999806 May 05 '16 at 07:10

10 Answers10

137

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.

galoget
  • 722
  • 9
  • 15
Swordsman
  • 1,789
  • 1
  • 11
  • 8
  • 1
    I don't know why but this doesn't work for me. I'm using python `3.5.2` – Gujarat Santana Apr 13 '18 at 04:31
  • 1
    changed import urllib to import urllib.request and nothing else and it started working again – Ayudh Apr 27 '18 at 06:27
  • still not quite sure why it works in that way but this does the trick, thanks. – Kuku Mar 20 '20 at 00:53
  • 2
    Some interesting background information can be found [here](https://bugs.python.org/issue36701). The same applies for example to `email.message` submodule. Basically the `os.path` is the only submodule in the stdlib that breaks this rule. – Jeyekomon Apr 23 '20 at 14:53
  • as of 2022-09-29, the links is dead. – João Ciocca Sep 29 '22 at 12:42
25

Use this way

import urllib.request
urllib.request.urlopen('http://216.58.192.142',timeout=1)
Kashif
  • 1,364
  • 17
  • 21
  • if one, like me, migrates the code from python2.7 to python3, changing previously used urllib2 to urllib this way will help too! – sunsetjunks Oct 31 '20 at 16:11
5

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

4

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
MPękalski
  • 6,873
  • 4
  • 26
  • 36
  • 7
    This will lead to confusion if another person ever needs to edit your code. – Boris Verkhovskiy Jun 21 '20 at 15:14
  • Probably not 'will' but 'may, but I agree if you write a code that may be managed by others it is better to write a comment about the workaround used, so it does not come with a surprise for maintainer. – MPękalski Jun 21 '20 at 21:32
2

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...

JohnnyFun
  • 3,975
  • 2
  • 20
  • 20
  • 1
    This fixed an issue ! What can also cause this error is if you have file named http.py anywhere in you working directory . Note that this can brake other libraries that depend on urllib ex. http.client – Ivan May 13 '19 at 16:37
0

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.

0

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

  • This is a duplicate of a number of existing answers. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers. Remember to review all existing answers first. – tjheslin1 Apr 07 '22 at 17:53
0

i did this and it worked for me :

from urllib3 import request

try that

AJX Tech
  • 1
  • 4
0

i have a same issue at using python version 3.8, but you can use urllib with http protocol (non https) like url = "http://www.google.com" for test maybe this just only used for http protocol request but not for https "correct me if i wrong thank you^" so how we can do that-:

import urllib.request

url = "http://www.google.com/"

headers = {'User-Agent': "duckduckgo.com-bot"}

request = urllib.request.Request(url, headers)
response = urllib.request.urlopen(request)

print(response)

reference document from python 3.8 -> (https://docs.python.org/3.8/library/urllib.request.html)

you can refer what you need -> ctrl+f urllib.request + urllib.request.urlopen

-3
import urllib.request as urllib

then in the code call urlopen function

example:

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