classification
Title: zipimporter find_module fullname mis-documented
Type: behavior Stage: test needed
Components: Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, cool-RR, eric.snow, gregory.p.smith, kmtracey, rfk, serhiy.storchaka, superluser, tjd.rodgers, xtreak
Priority: normal Keywords:

Created on 2010-04-14 15:20 by kmtracey, last changed 2019-05-16 17:45 by brett.cannon.

Messages (8)
msg103132 - (view) Author: Karen Tracey (kmtracey) Date: 2010-04-14 15:20
The fullname parameter to zipimporter find_module is documented to be a "fully qualified (dotted) module name". (From http://docs.python.org/library/zipimport.html#zipimport.zipimporter.find_module.) However, passing a fully-qualified dotted module appears to never work. Rather it appears that you must use the file system path separator instead of dots. For example on Windows:

C:\>echo %pythonpath%
\tmp\blah-1.0-py2.6.egg

C:\>python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from blah.models import speak
>>> speak.__file__
'C:\\tmp\\blah-1.0-py2.6.egg\\blah\\models\\speak.pyc'
>>> speak.sayhi()
Hi from blah.models.speak sayhi function.

The egg has top-level blah package and under it models, it appears to work fine for normal use.  But zipimport zipimporter find_module won't find the models sub-module using dotted path notation:

>>> import sys
>>> sys.path[1]
'C:\\tmp\\blah-1.0-py2.6.egg'
>>> import zipimport
>>> zi = zipimport.zipimporter(sys.path[1])
>>> zi.find_module('blah')
<zipimporter object "C:\tmp\blah-1.0-py2.6.egg">
>>> zi.find_module('blah.models')
>>> zi.find_module('blah/models')
>>> zi.find_module('blah\\models')
<zipimporter object "C:\tmp\blah-1.0-py2.6.egg">
>>>

On Linux, the 'blah/models' version is the one that works.

Tested as far back as Python2.3 and as far forward as 2.7a3.
msg109467 - (view) Author: Ryan Kelly (rfk) Date: 2010-07-07 10:05
Was also just bitten by this, trying to muck with PEP-302-style import hooks.  Note that the documented behaviour of zipimporter is also the behaviour required by PEP 302, i.e. full dotted module name.
msg134584 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-04-27 15:39
I was bitten now as well...
msg175527 - (view) Author: Te-jé Rodgers (tjd.rodgers) Date: 2012-11-13 21:03
I have also just been bitten by this. Is there anyway that this can be updated to support the dotted notation?
msg175565 - (view) Author: Te-jé Rodgers (tjd.rodgers) Date: 2012-11-14 13:05
It gets worse. Even though find_module works with the path separator, load_module fails.


>>> zi.find_module("lib\\ui")
<zipimporter object "dist/Test_Editor-1.0-py3.2.zip">
>>> zi.load_module("lib\\ui")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dist/Test_Editor-1.0-py3.2.zip\lib\ui\__init__.py", line 9, in <module>
  File "dist\Test_Editor-1.0-py3.2.zip\lib\ui\texteditorwindow.py", line 5, in <module>
  File "dist\Test_Editor-1.0-py3.2.zip\lib\ui\designer\__init__.py", line 5, in <module>
ValueError: Attempted relative import beyond toplevel package
msg175566 - (view) Author: Te-jé Rodgers (tjd.rodgers) Date: 2012-11-14 13:06
Disregard the last...error on my part (so embarrassing!)
msg342587 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-05-15 17:03
This still exists after zipimport rewrite in Python with 3.8 . Is this a documentation issue or an enhancement to be made as noted in msg109467 ?
msg342665 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-05-16 17:45
Actually find_module() should be deprecated and find_spec() should be defined instead (and the same goes for load_module(); see bpo-9699). So I'm personally fine w/ making this a doc problem w/ the plan to eventually deprecate the method.
History
Date User Action Args
2019-05-16 17:45:23brett.cannonsetmessages: + msg342665
2019-05-15 17:03:18xtreaksetnosy: + serhiy.storchaka, xtreak

messages: + msg342587
versions: + Python 3.7, Python 3.8, - Python 3.4, Python 3.5, Python 3.6
2015-08-05 16:01:37eric.snowsetversions: + Python 3.4, Python 3.5, Python 3.6, - Python 3.2
2015-08-05 16:01:20eric.snowsetnosy: + brett.cannon, gregory.p.smith, superluser
2012-11-14 13:06:25tjd.rodgerssetmessages: + msg175566
2012-11-14 13:05:14tjd.rodgerssetmessages: + msg175565
versions: - Python 2.7
2012-11-14 00:05:39eric.snowsetnosy: + eric.snow
2012-11-13 21:03:40tjd.rodgerssetnosy: + tjd.rodgers
messages: + msg175527
2011-04-27 15:39:57cool-RRsetnosy: + cool-RR
messages: + msg134584
2010-08-04 22:18:08terry.reedysetstage: test needed
versions: + Python 3.2, - Python 2.6, Python 2.5
2010-07-07 10:05:58rfksetnosy: + rfk
messages: + msg109467
2010-04-14 15:20:40kmtraceycreate