See All Titles |
![]() ![]() impThe imp module provides mechanisms to access the internal import statement implementation. You might want to use this module to overload the Python import semantics. Note that the ihooks module provides an easy-to-use interface for this task. imp.find_module()This function identifies the physical location of a given module name. basic syntax: file, path, desc = imp.find_module(modulename) imp.load_module()This one loads and returns a module object based on the information provided. basic syntax: obj = imp.load_module(modulename,file,path,desc) >>> import imp >>> def newimport(modulename): … file, path, desc = imp.find_module(modulename) … moduleobj = imp.load_module(modulename,file,path,desc) … return moduleobj … … math = newimport(math) … math.e 2.71828182846 imp.getsuffixes()It lists the precedence order in which files are imported when using the import statement. Typing the following commands in my environment accomplishes this: >>> import imp >>> imp.get_suffixes() [('.pyd', 'rb', 3), ('.dll', 'rb', 3), ('.py', 'r', 1), ('.pyc', 'rb', 2)] Note that if I have a module stored in a file called mymodule.pyc, and I enter the command import mymodule at the interpreter, the system initially searches for a file called mymodule.pyd, and then for one called mymodule.dll, one called mymodule.py, and finally it searches for a file called mymodule.pyc. Tip
When importing packages, this concept is ignored because directories precede all entries in this list.
|
Index terms contained in this sectionimp module 2ndimporting packages libraries Python Services modules imp 2nd packages importing Python Services syntax functions imp.find.module() imp.load.module() |
© 2002, O'Reilly & Associates, Inc. |