Python 3: AttributeError: object 'module' does not have attribute '__path__' using urllib in terminal

My code works fine in PyCharm, but I have error messages when I try to open it in the terminal. What happened to my code or where did I make mistakes?

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

Exit the 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 
+7
python urllib
source share
3 answers

You called the file C:\Users\Przemek\Desktop\urllib.py , you need to rename it. You are importing from this non-actual module. rename C:\Users\Przemek\Desktop\urllib.py and delete any C:\Users\Przemek\Desktop\urllib.pyc .

This is not the file you are using, but you have the file in the same directory, so python first checks the current directory, hence the error.

+9
source share

You will ensure that the standard urllib library package is urllib by naming your source urllib.py file. Rename it!

The fact that this generally works in Pycharm is an amazing feat of engineers for PyCharm developers!

Here you can also use absolute import ( from __future__ import absolute_import ); but in this case, I don’t think it will help, since the name of the launch source obscures the very library / package that you are trying to use!

0
source share

In addition, these are:

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

It should be like this:

 import urllib with urllib.urlopen('http://python.org/') as response: 
-one
source share

All Articles