Solved

Python shell versus script import urllib2

Posted on 2014-04-18
4
1,023 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 …
Six Sigma Control Plans

626 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