Python 3: AttributeError: 'module' object has no attribute '__path__' using urllib in terminal

淺唱寂寞╮ 提交于 2019-11-29 06:12:35

You called a file C:\Users\Przemek\Desktop\urllib.py, you need to rename it. You are importing from that not the actual module. rename C:\Users\Przemek\Desktop\urllib.py and remove any C:\Users\Przemek\Desktop\urllib.pyc.

It is not the file you are running but you have the file in the same directory so python checks the current directory first hence the error.

You sare shadowing the standard library package urllib by naming your source file urllib.py. Rename it!

The fact this works at all in Pycharm is an amazing feat of engineering on the PyCharm developers!

You can also use absolute imports (from __future__ import absolute_import) here; but in this case I don't think it'll help since your startup source name shadows the very library/package you are trying to use!

Also, this:

import urllib.request
with urllib.request.urlopen('http://python.org/') as response:

Should be like this:

import urllib
with urllib.urlopen('http://python.org/') as response:
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!