ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package

4.1k Views Asked by At

I have the following code and i tried all i could find on stackoverflow but nothing worked. Here is the code:

import urllib.request, urllib.parse, urllib.error

fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand:
    words = line.decode().split()
for word in words:
    counts[word] = counts.get(word, 0) + 1
print(counts)

And the error: ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package

5

There are 5 best solutions below

1
gorbachev On

do you have urllib installed? if not try to install it like this : pip install urllib3

0
Mihai On

I found the problem. I already had a file that was named urllib.py and that is why the error. Now that i deleted it everything works fine! Hope this answer will help everyone in need.

0
Edu On

This exact same issue happened to me. After trying and trying, I just ended up rewriting the code and saving the file into a new folder by itself and it ran without any problems.

Hope this helps!

2
Chrille On

re-named my urllib-named file, that helped me too as Dharman stated.

0
Gareleon On

My file name was urllib.py, changed it to workbitch.py and now it works perfectly! ;)

Hope this helps!