About new urllib.request in python 3.1.2

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jul 10 11:35:06 EDT 2010


On Sat, 10 Jul 2010 07:06:47 -0700, pcchen wrote:

> And for the following three simple lines of code, borrowed from official
> python-doc 3.1.2:
> 
>>>>from urllib.request import urlopen
>>>>response = urlopen('http://python.org/') html = response.read()
> 
> 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.


-- 
Steven



More information about the Python-list mailing list