Python 3:AttributeError:'module'对象在终端中使用urllib没有属性'__path__'

Prz*_*mek 12 python urllib

我的代码在PyCharm中完美运行,但是在尝试在终端中打开它时我有错误消息.我的代码有什么问题,或者我犯了错误?

import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
   html = response.read()
   print(html)
Run Code Online (Sandbox Code Playgroud)

终端输出:

? python Desktop\url1.py
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, 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 "Desktop\url1.py", line 1, in <module>
    import urllib.request
  File "C:\Users\Przemek\Desktop\urllib.py", line 1, in <module>
    import urllib.request
ImportError: No module named 'urllib.request'; 'urllib' is not a package
Run Code Online (Sandbox Code Playgroud)

Pad*_*ham 12

你调用了一个文件C:\Users\Przemek\Desktop\urllib.py,你需要重命名它.您是从那个而不是实际模块导入的.重命名C:\Users\Przemek\Desktop\urllib.py并删除任何C:\Users\Przemek\Desktop\urllib.pyc.

它不是您正在运行的文件,但是您将文件放在同一目录中,因此python首先检查当前目录,从而导致错误.