Solved

Python shell versus script import urllib2

Posted on 2014-04-18
4
997 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 28

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 28

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now