Python Forum
urllib is not a package traceback
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
urllib is not a package traceback
#1
Hello! I'm in desperate need of help.

I'm attempting to run the following code:
import urllib.request, urllib.parse, urllib.error

fhand = urllib.request.urlopen('http.//data.pr4.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())
and come up with the following:
Error:
File "libby.py", line 1, in <module> import urllib.request, urllib.parse, urllib.error File "/Users/claudiachopek/Desktop/py4e/urllib.py", line 1, in <module> import urllib.request, urllib.parse, urllib.error ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package
If it's of any help, I'm running Python 3.8.5 on Mac OS Catalina 10.15.5

Can someone please tell me why this is happening and how to fix it? I've been doing pretty well with the lessons up till now but can't complete any exercises or examples until I get this figured out.

Thank you in advance!
Reply
#2
your URL has a '.' where a ':' is needed fhand = urllib.request.urlopen('http.//data.pr4.org/romeo.txt')
should be: fhand = urllib.request.urlopen('http://data.pr4.org/romeo.txt')
Reply
#3
You have a file in your path called urllib.py, and that is interfering with the urllib module.

Remove or rename /Users/claudiachopek/Desktop/py4e/urllib.py and the imports should work correctly.
Reply
#4
Also Requests has taken over what urllib dos in a better way,i have not used urllib in many year,other than sometime help on forum users that use urllib.
Also loop over online .txt like this,you may get new line in wrong place.
import requests

fhand = requests.get('https://www.py4e.com/code/romeo.txt')
for line in fhand:
    print(line.decode().strip())
Output:
But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon W ho is already sick and pale with grief
Just do this.
import requests

fhand = requests.get('https://www.py4e.com/code/romeo.txt')
print(fhand.text.strip())
Output:
But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  urllib can't find "parse" rjdegraff42 6 2,221 Jul-24-2023, 05:28 PM
Last Post: deanhystad
  Help with urllib.request Brian177 2 2,887 Apr-21-2021, 01:58 PM
Last Post: Brian177
  urllib.request ericmt123 2 2,456 Dec-21-2020, 06:53 PM
Last Post: Larz60+
  urllib request error 404 Coco 2 4,421 May-11-2019, 02:47 PM
Last Post: Larz60+
  KeyError urllib DavidFernandez 4 3,591 Nov-21-2018, 08:34 PM
Last Post: DavidFernandez
  URLLIB.REQUEST Not Working hallofriends 1 6,403 Sep-18-2017, 05:00 PM
Last Post: Larz60+
  how to loop data in urllib? pythonlover 4 7,754 Jan-18-2017, 06:53 PM
Last Post: pythonlover

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020