Jan
Jan

Reputation: 11

Urllib not working

I have Python 2.7.10 on Windows 7. Urllib works in IDLE shell no problem, however when I run the exact same code from lets say a file, or from python urltest.py

My code:

import urllib
page = urllib.urlopen("http://www.google.com")
contents = page.read()
print contents

I get the following error:

page = urllib.urlopen("http://www.google.com")
AttributeError: 'module' object has no attribute 'urlopen'

Keep in mind that this exact code works in IDLE shell (shell, not editor)

Upvotes: 0

Views: 846

Answers (2)

shivankgtm
shivankgtm

Reputation: 1242

#python3
import urllib.request page = urllib.request.urlopen("http://www.google.com")

the error is coming because when you run python file_name.py it's calling python3. type python2 file_name.py, it will work.

# python2 import urllib page = urllib.request.urlopen("http://www.google.com")

Upvotes: 0

Nitesh Patel
Nitesh Patel

Reputation: 631

I would suggest your default Python is version 3, that no longer has urllib.urlopen (it uses urllib.request.urlopen).

IDLE is likely using 2.7.

Upvotes: 2

Related Questions