天天看点

python导入urllib request_Python 3.3 - urllib.request - 导入错误

当我尝试在PyCharm 2.7中的OS X 10.8上运行以下Python 3.3代码(或使用Python 3.3/2.7.3启动程序运行.py文件)时:

import urllib.request

f = urllib.request.urlopen('http://www.python.org/')

print(f.read(300))

我收到以下错误消息:

/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /Users/username/PycharmProjects/urllib/urllib.py

Traceback (most recent call last):

File "", line 1512, in _find_and_load_unlocked

AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in

import urllib.request

File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in

import urllib.request

ImportError: No module named 'urllib.request'; urllib is not a package

Process finished with exit code 1

我可以成功运行代码的唯一方法是通过Python shell.

关于如何解决这个问题的任何想法?

谢谢.

我将文件名更改为url.py,现在它在PyCharm中成功执行.

但是当通过Python Launcher 3.3执行文件时,它给出了以下错误:

File "/Users/username/PycharmProjects/urllib/url.py", line 3, in

import urllib.request

ImportError: No module named request

为什么PyCharm(3.3)中的代码运行正常但是在使用Python Launcher(3.3)启动时给出了错误?