Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python shell versus script import urllib2

Posted on 2014-04-18
4
Medium Priority
?
1,042 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 20

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

783 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