About new urllib.request in python 3.1.2

P

pcchen

Sorry I have searched related topic but none could point out this
problem.

I am currently using python 3.1.2:
Python 3.1.2 (r312:79147, Jun 30 2010, 11:58:11)
[GCC 4.2.1 20070719 [FreeBSD]] on freebsd8

And for the following three simple lines of code, borrowed from
official python-doc 3.1.2:

They could cause this error:

File "./http.py", line 3, in <module>
from urllib.request import urlopen
File "/usr/local/lib/python3.1/urllib/request.py", line 88, in
<module>
import http.client
File "/home/pcchen/Python/http.py", line 3, in <module>
from urllib.request import urlopen
ImportError: cannot import name urlopen

All the related discussions point to urlopen has been moved from
urllib to urllib.request, but none can discribe the above error.

Any information is appreciated, thank you so much.
 
S

Steven D'Aprano

And for the following three simple lines of code, borrowed from official
python-doc 3.1.2:


They could cause this error:

File "./http.py", line 3, in <module>
from urllib.request import urlopen
File "/usr/local/lib/python3.1/urllib/request.py", line 88, in
<module>
import http.client
File "/home/pcchen/Python/http.py", line 3, in <module>
from urllib.request import urlopen
ImportError: cannot import name urlopen

Look at the traceback: your code executes "from urllib.request import
urlopen". That line in turn executes "import http.client". And *that*
fails, which causes the first import to fail.

It fails because you have shadowed the built-in package http with your
own module http.py.

Rename your file to something else ("myhttp.py") and it should just work.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top