KeyError: 'urllib.parse'

265 views
Skip to first unread message

Joseph Tate

unread,
Feb 17, 2012, 3:41:56 PM2/17/12
to PyInstaller
I'm trying to build a onedir exe on windows, but I'm running into
trouble with cherrypy;

Cherrypy has this bit of code:

try:
# Python 3
from urllib.parse import urljoin, urlencode
from urllib.parse import quote, quote_plus
from urllib.request import unquote, urlopen
from urllib.request import parse_http_list, parse_keqv_list
except ImportError:
# Python 2
from urlparse import urljoin
from urllib import urlencode, urlopen
from urllib import quote, quote_plus
from urllib import unquote
from urllib2 import parse_http_list, parse_keqv_list

but my built package fails with this:
...
File iu.py, line 455, in importHook
del sys.modules[fqname]
KeyError: 'urllib.parse'

It's eating up the import error prematurely. Any way to fix this?

Joseph Tate

unread,
Feb 17, 2012, 9:03:15 PM2/17/12
to pyins...@googlegroups.com
I patched the iu.py so that it checks that fqname is in sys.modules before del'ing it and I can get it to build and run now.

Martin Zibricky

unread,
Feb 18, 2012, 4:33:02 AM2/18/12
to pyins...@googlegroups.com
Joseph Tate píše v Pá 17. 02. 2012 v 18:03 -0800:

> I patched the iu.py so that it checks that fqname is in sys.modules
> before del'ing it and I can get it to build and run now.

Could you please submit your patch?

http://www.pyinstaller.org/wiki/Development/HowtoContribute

Thanks in advance.

jssebastian

unread,
Apr 3, 2012, 2:37:48 PM4/3/12
to PyInstaller
I had the same problem as Joseph, because url.parse is python 3, I am
using python 2, and some library I use must have a try-catch for the
import like the one in his code.

This mini-patch against the current release (1.5.1) of iu.py seems to
solve the problem, at least getting my program to start (don't see how
to add an attachment in groups interface):

454c454,455
< del sys.modules[fqname]
---
> if fqname in sys.modules:
> del sys.modules[fqname]

Not sure if it creates other problems.

ciao,
Paolo

Hartmut Goebel

unread,
Apr 4, 2012, 4:12:04 AM4/4/12
to pyins...@googlegroups.com
On Feb 18, 2:33 am, Martin Zibricky <mzibri...@gmail.com> wrote:
>> Joseph Tate píše v Pá 17. 02. 2012 v 18:03 -0800:
>>
>>> I patched the iu.py so that it checks that fqname is in sys.modules
>>> before del'ing it and I can get it to build and run now.
>> Could you please submit your patch?

No need for a patch, this is already fixed in 2.0dev.

--
Schönen Gruß - Regards
Hartmut Goebel
Dipl.-Informatiker (univ.), CISSP, CSSLP

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de

Monatliche Kolumne: http://www.cissp-gefluester.de/
Goebel Consult ist Mitglied bei http://www.7-it.de


Martin Zibricky

unread,
Apr 4, 2012, 4:22:30 AM4/4/12
to pyins...@googlegroups.com
Thanks Polo.

It should be fixed in develop branch.

jssebastian píše v Út 03. 04. 2012 v 11:37 -0700:

Hartmut Goebel

unread,
Apr 4, 2012, 4:26:31 AM4/4/12
to pyins...@googlegroups.com, jssebastian
Am 03.04.2012 20:37, schrieb jssebastian:
> 454c454,455
> < del sys.modules[fqname]
> ---
>> if fqname in sys.modules:
>> del sys.modules[fqname]

The Python idiom for this is using try/except: "don't ask for permission
but for forgiveness"

Reply all
Reply to author
Forward
0 new messages