The imp Module

The imp module contains functions that can be used to implement your own import behavior. Example 13-6 overloads the import statement with a version that logs from where it gets the modules.

Example 13-6. Using the imp Module
File: imp-example-1.py

import imp
import sys

def my_import(name, globals=None, locals=None, fromlist=None):
 try:
 module = sys.modules[name] # already imported?
 except KeyError:
 file, pathname, description = imp.find_module(name)
 print "import", name, "from", pathname, description
 module = imp.load_module(name, file, pathname, description)
 return module

import _ _builtin_ _
_ _builtin_ _._ _import_ _ = my_import

import xmllib

import xmllib from /python/lib/xmllib.py (.py, 
, 1)
import re from /python/lib/re.py (.py, 
, 1)

import sre from /python/lib/sre.py (.py, 
, 1)
import sre_compile from /python/lib/sre_compile.py (.py, 
, 1)
import _sre from /python/_sre.pyd (.pyd, 
b, 3)

Note that the alternative version shown here doesn support packages. For a more extensive example, see the sources for the knee module.


Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

show all menu





Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net