I am learning python. Today I meet with a odd problem.
from urllib import urlopen
url='http://www.google.com'
f=urlopen(url).read()
print f
It is a sample script ,it can run if it in C partition however in D partition
it has AttributeError:
Traceback (most recent call last):
File "D:\urlopen.py", line 1, in <module>
from urllib import urlopen
File "D:\urllib.py", line 7, in <module>
nettext=urllib.urlopen(strurl).read()
AttributeError: 'module' object has no attribute 'urlopen'
I installed python2.7 and python3.1 in win7,and I run the script in python2.7’s shell.
I don’t know why it works on
C:\. It should fail either way. You’re importing something fromurllib. Your script is calledurllib. The current directory comes before standard library dirs, so you import yourself. It’s only because imports are “cached” (a secondimport xin the same interpreter process just gives a reference to the already imported module instead of loading it again) that this doesn’t lead to an infinite loop. Of course your module doesn’t have anything that’s in the stdliburllibpackage, e.g. nourlopen.