[issue11676] Improve imp.load_module and submodules doc

Terry J. Reedy report at bugs.python.org
Sat Apr 2 01:39:29 CEST 2011


Terry J. Reedy <tjreedy at udel.edu> added the comment:

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."

----------
nosy: +terry.reedy
title: imp.load_module and submodules - doc issue, or bug? -> Improve imp.load_module and submodules doc
versions: +Python 3.1, Python 3.2, Python 3.3 -Python 2.5, Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11676>
_______________________________________


More information about the Python-bugs-list mailing list