Solved

Python shell versus script import urllib2

Posted on 2014-04-18
4
1,012 Views
Last Modified: 2014-04-21
I can enter the following directly into a Python Shell with success:
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
# code 
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()

#error received when run .py script:
Traceback (most recent call last):
  File "C:\Help\Training\Python\web\urllib2.py", line 5, in <module>
    import urllib2
  File "C:\Help\Training\Python\web\urllib2.py", line 6, in <module>
    response = urllib2.urlopen('http://python.org/')
AttributeError: 'module' object has no attribute 'urlopen'

Open in new window


When I save the same lines into a .py script and run, I receive this error:
Traceback (most recent call last):
  File "C:\Help\Training\Python\web\urllib2.py", line 5, in <module>
    import urllib2
  File "C:\Help\Training\Python\web\urllib2.py", line 6, in <module>
    response = urllib2.urlopen('http://python.org/')
AttributeError: 'module' object has no attribute 'urlopen'

Looking for explanation on why the error.  Any help please ?
Thanks
0
Comment
Question by:DoveTails
  • 2
4 Comments
 
LVL 19

Expert Comment

by:simon3270
ID: 40010207
I've tried this in Linux (Python 2.7.5), and all works well (with python shell, and from a file).  Also from Windows (Python 2.7.6).

The error says the import is on line 5 - what do the first 4 lines say?

Just tried it in Python 3.4 on Windows, but I don't have the urllib2 module (and neither urllib nor urllib3 have a urlopen method).

(edit: added correct Windows Python2 version, and that Python3 fails)
0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 40010722
Do not name your own script urllib2.py. Add or change whatever part of the name. Python tries to import your own script as the urllib2. After changing it to say urllibX.py, do not forget to remove the earlier precompiled urllib2.pyc.

In Pyhon 3,  the doc says (https://docs.python.org/2/library/urllib2.html):
The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
0
 
LVL 5

Author Closing Comment

by:DoveTails
ID: 40012904
Thank you Pepr - after renaming the script and deleting the compiled file, all worked as expected.   Simple solution that would've taken me a long time to figure out ... appreciate your input.  Huge time saver for me.
Thanks Simon3270 for testing, Pepr's solution solved my dilemma.
0
 
LVL 29

Expert Comment

by:pepr
ID: 40013043
I am glad I could help. :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 123
wipe a usb using python 5 83
incorrect syntax near the order by 10 56
IDE for Python 5 62
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question