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
  MPEG DASH Package implementation similar to M3U8 package anantha_narayanan 1 2,038 May-05-2025, 08:34 AM
Last Post: pintailscratchy
  urllib can't find "parse" rjdegraff42 6 9,776 Jul-24-2023, 05:28 PM
Last Post: deanhystad
  Help with urllib.request Brian177 2 4,685 Apr-21-2021, 01:58 PM
Last Post: Brian177
  urllib.request ericmt123 2 3,627 Dec-21-2020, 06:53 PM
Last Post: Larz60+
  Cannot open url link using urllib.request Askic 5 9,905 Oct-25-2020, 04:56 PM
Last Post: Askic
  urllib request error 404 Coco 2 6,060 May-11-2019, 02:47 PM
Last Post: Larz60+
  KeyError urllib DavidFernandez 4 5,249 Nov-21-2018, 08:34 PM
Last Post: DavidFernandez
  URLLIB.REQUEST Not Working hallofriends 1 7,480 Sep-18-2017, 05:00 PM
Last Post: Larz60+
  how to loop data in urllib? pythonlover 4 9,684 Jan-18-2017, 06:53 PM
Last Post: pythonlover

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.