I came across this item while programming something that should have been very simple. VLC Media Player will save the last viewed position in a media file (assuming you have the resume option enabled). It maintains two lists, file names, and offset times. The file names are encoded such that certain characters are replaced with "%" strings (%20 for a blank, for example). I wrote a short script to remove the resume entries for files that were no longer on my computer.

The Python urllib has two handy methods for encoding/decoding these strings within URLs. In my code I had

import urllib
.
.
.
file = urllib.parse.unquote(file_list[i])

I developed the code using idlex (an idle variant). Once the code was debugged I attached it to a hotkey. Much to my surprise, running the code with the hotkey did nothing. Stripping it down to the bare essentials gave me

import urllib

print(urllib.parse.unquote('my%20file.txt'))

Running it from within idlex resulted in

======================= RESTART: D:\temp\test.py =======================
my file.txt

but running it from a cmd shell gave me

Traceback (most recent call last):
  File "D:\temp\test.py", line 3, in <module>
    print(urllib.parse.unquote('my%20file.txt'))
AttributeError: module 'urllib' has no attribute 'parse'

From the idlex shell, typing dir(urllib) gives

>>> dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'parse']

and running the same from Python invoked in a cmd shell gives

>>> dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

The idlex environment adds parse. I found the same addition when I edited and ran the code in the Visual Studio Code IDE.

The solution (if you can call it that) was to code it as

from urllib import parse

print(parse.unquote('my%20file.txt'))

I have found no explanation for this behaviour, nor any reason why an IDE would provide an environment that does not accurately reflect a production environment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.