This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve imp.load_module and submodules doc
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Arfrever, Dave Peck, Trundle, brett.cannon, docs@python, terry.reedy
Priority: normal Keywords:

Created on 2011-03-25 21:40 by Dave Peck, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Repositories containing patches
https://davepeck@bitbucket.org/davepeck/pybug
Messages (6)
msg132162 - (view) Author: Dave Peck (Dave Peck) Date: 2011-03-25 21:40
If you use `import` to load a package and subpackage:

    import package
    import package.subpackage
  
Then the `package` module instance will contain a `subpackage` attribute:

    assert "subpackage" in dir(sys.modules['package']), "This works."
  
But if you use Python's `imp` module to import these packages instead, the same assertion will fail.

Is this a python documentation oversight, or a bug with the `imp` module? 

To reproduce, clone the associated hg repro and follow the instructions in the README file. Thanks!
msg132416 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2011-03-28 20:09
I say it's a documentation bug.
msg132443 - (view) Author: Dave Peck (Dave Peck) Date: 2011-03-28 22:52
Definitely could agree with this assessment, but it is surprising given the lack of parallel behavior between 'import' and 'imp'.

Note that imp.load_module(subpackage) _does_ modify the parent module's attributes -- but it will never put the subpackage attribute itself on parent. That's the part that made me think it shouldn't work this way and that its more bug than documentation. (That, and the specific use of the imp module in Google's dev_appserver.py which indicates that others have the same expectation of load_module() that I did.)
msg132490 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2011-03-29 17:07
It's actually not surprising that imp works this way: it predates packages.

Because the semantics (I assume) have been like this for ages I say we document the current behavior (it's easy to work around) and simply continue to replace imp functionality with importlib ones that are more modern and reasonable.
msg132779 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-04-01 23:39
I verified this for 3.2 (and IDLE) with

import sys, tkinter
"ttk" in dir(sys.modules['tkinter']) # False
import tkinter.ttk
"ttk" in dir(sys.modules['tkinter']) # True

====reload========

import sys,imp
imp.load_module('tkinter',*imp.find_module('tkinter'))
imp.load_module('tkinter.ttk',*imp.find_module('ttk',
  sys.modules["tkinter"].__path__))
"ttk" in dir(sys.modules['tkinter']) # False
from tkinter import ttk # ImportError

Given that 'ttk' is only added to the tkinter entry when ttk is imported via tkinter, I am not surprised that the direct import with imp fails to affect the tkinter entry. Imp would have to notice that ttk is in a directory with __init__, get the name of the parent directory, and then check to see whether or not that parent had already been imported. Unlike import

import ttk #fails

imp does not require such a previous import:

imp.load_module('ttk',*imp.find_module('ttk',
 ["C:/programs/python32/Lib/tkinter"]))

succeeds, which shows again that import and imp act differently.

Dave, can you suggest added text and a location to put it?

Hmmm. How about after "If the load ...", add "A successful load does not affect previously loaded package modules as this function operates independently of package structure."
msg180614 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-01-25 19:32
imp.find_module() is now deprecated, so not worrying about adding more details to the docs for the function.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55885
2013-01-25 19:32:38brett.cannonsetstatus: open -> closed
resolution: out of date
messages: + msg180614
2011-09-25 21:14:07Arfreversetnosy: + Arfrever
2011-04-01 23:39:28terry.reedysetnosy: + terry.reedy
title: imp.load_module and submodules - doc issue, or bug? -> Improve imp.load_module and submodules doc
messages: + msg132779

versions: + Python 3.1, Python 3.2, Python 3.3, - Python 2.6, Python 2.5
2011-03-29 17:07:39brett.cannonsetmessages: + msg132490
2011-03-29 03:53:11Trundlesetnosy: + Trundle
2011-03-28 22:52:04Dave Pecksetmessages: + msg132443
2011-03-28 20:09:29brett.cannonsetnosy: + docs@python, brett.cannon
messages: + msg132416

assignee: docs@python
components: + Documentation, - Interpreter Core, Library (Lib)
stage: needs patch
2011-03-25 21:40:26Dave Peckcreate