Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
urllib can't find "parse"
#1
I don't know if it's just me having a "senior" moment, but when I run the following code in idle

import urllib

print(urllib.parse.quote('my file.txt'))
I get the output

my%20file.txt
but when I run the same code from the command line I get

Traceback (most recent call last):
  File "D:\temp\test.py", line 3, in <module>
    print(urllib.parse.quote('my file.txt'))
AttributeError: module 'urllib' has no attribute 'parse'
the following (from the command line) seems to show that parse is indeed part of the package

>>> help(urllib)
Help on package urllib:

NAME
    urllib

MODULE REFERENCE
    https://docs.python.org/3.10/library/urllib.html

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

PACKAGE CONTENTS
    error
    parse
    request
    response
    robotparser

FILE
    c:\python\lib\urllib\__init__.py
I'm running Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32

Windows 11 Home.
AttributeError: module 'urllib' has no attribute 'parse'

FYI I have ten different folders all named urllib. Everything that wants to use Python seems to install its own copy and I have no way of knowing what gets used when I run a script inside, or outside idle.
Reply
#2
Maybe this link will help. Urllib is for internet/network urls. If you are wanting to read a local file use pythons' with open(file, 'r') as var
https://docs.python.org/3/library/urllib...llib.parse
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
I'm not trying to read a file. I just want to quote/unquote the file strings. I do not understand why it works in idle but not stand-alone.

I did find a work-around

from urllib import parse
print(parse.quote('my file.txt')
but I'd still like to know why the discrepancy.
Reply
#4
Just because modules share a namespace or package doesn't mean you can import the top of the namespace and see everything. In this case, urllib isn't a module and it doesn't do anything useful if imported. You need to import the urllib.parse module (or bring in functions from inside).

import urllib # Not useful
import urllib.parse # OK
from urllib import parse # OK
from urllib.parse import quote # OK if you just need the quote function.
I'm not familiar enough with idle to know why it's different when running there.
Reply
#5
It doesn't surprise me that IDLE makes your program act differently. IDLE runs your program inside of IDLE, changing things here and there to intercept things here and override things there to keep IDLE informed about what is going on. For example, IDLE replaces stdin and stdout with IDLE specific versions. A few months ago there was a post on the forum asking why their program, which worked fine when run form the command line failed when run from IDLE. The reason was that the IDLE stdout did not support some features that have become standard in newer versions of Python. There are many posts on the forum wondering why tkinter programs run differently when using IDLE (the programs never exit because they are running inside the IDLE shell). It didn't take long for me to get tired of all the IDLE weirdness and move on to a different IDE.

I couldn't find where IDLE imports parse when your program imports urllib. I dug around for half an hour and lost interest. All I know is in IDLE:
Output:
import urllib dir(urllib)[/python] ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'parse']
From the CMD shell
Output:
c:\>python Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib >>> dir(urllib) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
Reply
#6
Definitely strange behaviour. I tried using the Visual Studio Code IDE and it gives this result

>>> dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'parse']
It seems to me that the IDE should not significantly change the environment such that functioning code (from within the IDE) becomes non-functioning when run outside the IDE. But at least I know how to fix it (this time). I can't wait for the next gotcha.
Reply
#7
I use VSCode and it does not do that for me. This is the program:
import urllib

print(dir(urllib))
This is the output:
Output:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
No parse.

Change program to this:
import urllib.parse

print(dir(urllib))
And now, as expected, I get parse.
Output:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'parse']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  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 is not a package traceback cc26 3 5,423 Aug-28-2020, 09:34 AM
Last Post: snippsat
  urllib request error 404 Coco 2 4,420 May-11-2019, 02:47 PM
Last Post: Larz60+
  KeyError urllib DavidFernandez 4 3,588 Nov-21-2018, 08:34 PM
Last Post: DavidFernandez
  urlparse to urllib.parse - the script stopped working apollo 5 6,692 Oct-26-2017, 06:57 AM
Last Post: apollo
  URLLIB.REQUEST Not Working hallofriends 1 6,400 Sep-18-2017, 05:00 PM
Last Post: Larz60+
  how to loop data in urllib? pythonlover 4 7,752 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