From: pcchen on
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:

>>>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

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.
From: Steven D'Aprano on
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
From: Cameron Simpson on
On 10Jul2010 09:11, pavan kumar maddali <pavan_maddali(a)yahoo.com> wrote:
| Thank You Steve,
| I am not using the urllib.

Your example did:

from urllib.request import urlopen
response = urlopen('http://python.org/') html =
response.read()

| I am using the xmlrpc and http modules from the
| python library.
[...]

That's not what he meant.

Your file is called "http.py".
Python searches the current directory first.
That means that when any library module (internally) tried to use
the "http" module it will find your file, not the library module.

So you don't get to use the http library module.

Rename your http.py file to "my-http-test.py" and retry.
Then its name won't get in the way of the library name.

Cheers,
--
Cameron Simpson <cs(a)zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Success in software development depends on making a carefully planned
series of small mistakes in order to avoid making unplanned large
mistakes. - Steve McConnell, _Software Project Survival Guide_