Help with Python--ImportError

angie

Beta member
Messages
3
Location
United States
Hello,

I'm teaching myself Python as my first programming language. The book says to type:
import urllib.request
import time

for the first two lines of code.

Whenever I type import urllib.request for the first line,
I get this error:

Traceback (most recent call last):
File "C:/Users/...", line 1, in <module>
import urllib.request
ImportError: No module named request


I'm really confused why this isn't working. I downloaded Python 3. However, when I select Run Shell, it says Python 2.7.6. I doubled checked and made sure that I downloaded Python 3 though.

Do you have any suggestions on how I can get this code to work?
 

strollin

Knowitall!
Messages
3,565
Location
N. Calif.
What folder is python installed in? If the folder is named \python2x then you have python 2.x installed, if the folder is named python3x then you have python 3 installed.

urllib.request is only available in 3, prior to that the library was simply urllib.

See here: https://docs.python.org/2/library/urllib.html

Note: The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3. Also note that the urllib.request.urlopen() function in Python 3 is equivalent to urllib2.urlopen() and that urllib.urlopen() has been removed.
 
Last edited:

angie

Beta member
Messages
3
Location
United States
So I checked which Python I installed, and it's 3. But your point also caused me to think about checking which version of Idle I was using. It turns out, on Windows 8 when I search for Idle, the first option it gives me is with Python 2, the second option is with Python 3. So I was chose the second option, and now it's working.
 
Top