76

When I run the following code in Python 3.3:

import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")

I get the following error:

enter image description here

I did this too to verify:

enter image description here

What am I doing wrong?

jkdev
  • 9,037
  • 14
  • 52
  • 75
Pruthvi Raj
  • 2,898
  • 1
  • 20
  • 36
  • 1
    Are you sure you're running it in Python 3? – Ashwini Chaudhary Mar 09 '14 at 06:03
  • Yes I am sure!!, I've tried running it on 3.2 and 3.4 as well. No Luck :( – Pruthvi Raj Mar 09 '14 at 06:04
  • 11
    Maybe try `import urllib.request` directly? It [doesn't appear](http://hg.python.org/cpython/file/7ff62415e426/Lib/urllib/__init__.py) that `urllib` will pull in its sub-modules itself with 3.3.x. – Jonathan Lonowski Mar 09 '14 at 06:14
  • @JonathanLonowski Thanks man! you should have posted that as answer so i could it accept it ;) – Pruthvi Raj Mar 09 '14 at 06:16
  • This error also appears when you have a file in the same directory named as a standard module. Annoying but true. Be careful! – Nightforce2 Oct 16 '18 at 19:38
  • Had the same issue just now. When first importing urllib, dir(urllib) does not show 'request'. The weird thing is, when I go to help() >> modules >> quit >> dir(urllib), the 'request' method shows up. – techfour9 Sep 03 '19 at 23:08

4 Answers4

151

The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.


Import urllib.request instead of urllib.

import urllib.request
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • 1
    I've made sure and tried it on other 3.x versions as well, also added a picture in the question to make sure that there is urllib module. still no luck :3 – Pruthvi Raj Mar 09 '14 at 06:12
  • 2
    For some reason my code works with `import urllib` on one machine, while the other does require `import urrlib.request`.. both run 3.4.2. Anyway, this worked wonders! – Joost Apr 22 '15 at 22:44
  • @Joost, Check the version you're using by run `python -V`. or put following line into the script and run it: `import sys; print(sys.version)` – falsetru Apr 22 '15 at 23:27
  • what a twisted name for a module, when another module 'urllib' also exists. I guess it is for some backwards compatibility, but still.. – Valentas Oct 13 '16 at 20:15
  • i tried with urllib.request, the error changed to "No module named request" – rodrigorf Apr 27 '17 at 00:01
  • @rodrigorf, Maybe you're using Python 2.x. Or check whether there's `urllib.py` in the working directory; that will prevent import of standard library `urllib`. In such case, rename `urllib.py` (remove `urllib.pyc` if there is one) – falsetru Apr 27 '17 at 01:12
  • Thanks @falsetru, the problem was exactly the version of Heroku. I forced the runtime version on Heroku to use 3.4.3. Its working now. – rodrigorf Apr 27 '17 at 02:40
  • Why does this happen? – johan Jan 30 '20 at 08:49
  • 2
    @johan, because importing a packge does not automatically import its submodule. (some does, like os.path ...) – falsetru Jan 30 '20 at 12:52
  • 1
    Although this works, it may be better to explain why `import urllib` does not work so that the reader gains more understanding of this problem. – jdhao Jun 15 '20 at 12:26
  • @jdhao, I added explanation. Thank you for feedback. – falsetru Jun 15 '20 at 13:45
3

Interestingly, I noticed some IDE-depending behavior.

Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do

import urllib.request

while in Spyder,

import urllib

does fine

JB Lepetit
  • 171
  • 1
  • 9
  • Even more weird is running in the interpreter. import urllib; urllib.request will give you the expected AttributeError, but then run urllib.request again, and it's there! It seems that in the interpreter, after throwing an AttributeError, it implicitly imports all the submodules anyway. Very strange. – Sam Bull Jun 14 '17 at 15:26
1

If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.

Anonymous Person
  • 1,217
  • 6
  • 21
  • 42
0
  • In visual code , u have to write import urllib.request instead of just import urllib.
  • Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
  • So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.
Ashraf
  • 1