Solved

Python shell versus script import urllib2

Posted on 2014-04-18
4
1,010 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

828 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