Python 3.3 - urllib.request - imp

2024-04-25 19:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试在PyCharm 2.7中的OSX10.8上运行以下Python3.3代码时(或者使用Python3.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 "<frozen importlib._bootstrap>", 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 <module>
    import urllib.request
  File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in <module>
    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 <module>
import urllib.request
ImportError: No module named request

为什么PyCharm(3.3)中的代码运行良好,但在使用PythonLauncher(3.3)启动时出错?


Tags: 文件代码inpyimportrequest错误line