Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

urllib

My code is runnning perfectly in PyCharm, but I have error messages while trying to open it in terminal. What's wrong with my code, or where I made mistakes?

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

Output from terminal:

λ 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
like image 873
Przemek Avatar asked May 15 '15 14:05

Przemek


1 Answers

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!

like image 81
James Mills Avatar answered Oct 19 '22 16:10

James Mills