a newbie question

Z

zhangle2002

hi,

I am just learning Python and get a problem when trying this example:

from urllib import urlopen
doc=urlopen("http://www.python.org").read()
print(doc)

when i run this, i was tould that 'cannot import name "urlopen"


What's wrong with this code? Do i need to place my code in some
specific directory? Thanks a lot. '
 
E

Eugene Perederey

use urllib2

2009/4/12 said:
hi,

I am just learning Python and get a problem when trying this example:

from urllib import urlopen
doc=urlopen("http://www.python.org").read()
print(doc)

when i run this, i was tould that 'cannot import name "urlopen"


What's wrong with this code? Do i need to place my code in some
specific directory? Thanks a lot. '
 
P

Peter Otten

I am just learning Python and get a problem when trying this example:

from urllib import urlopen
doc=urlopen("http://www.python.org").read()
print(doc)

when i run this, i was tould that 'cannot import name "urlopen"


What's wrong with this code? Do i need to place my code in some
specific directory? Thanks a lot. '

This function has been moved in Python 3.0. Change your import to

from urllib.request import urlopen

If you are learning with a book using Python 2.x consider switching to
Python 2.5 or 2.6 to try out the examples.

Peter
 
L

larryzhang

This function has been moved in Python 3.0. Change your import to

from urllib.request import urlopen

If you are learning with a book using Python 2.x consider switching to
Python 2.5 or 2.6 to try out the examples.

Peter

It works. Thanks a lot Peter.

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,501
Members
48,189
Latest member
StaciLgf76

Latest Threads

Top