import hook

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Sun Jun 18 15:54:58 EDT 2006


Thomas Heller wrote:

> There are also other ways.  You could extend __path__ of foo, and the
> pkgutil module might also be useful.

The __path__ trick worked nicely, thanks. Here is the code in case anyone is
interested

# Allow veusz to be run even if not installed into PYTHONPATH
try:
    import veusz
except ImportError:
    # load in the veusz module, but change its path to
    # the veusz directory, and insert it into sys.modules
    import __init__ as veusz
    thisdir = os.path.dirname( os.path.abspath(__file__) )
    veusz.__path__ = [thisdir]
    veusz.__name__ = 'veusz'
    sys.modules['veusz'] = veusz

This is part of the main program. If it can't import it (i.e. it is not
installed), it imports the __init__ module, renames it, and corrects its
path, then sticks it into the list of imported modules.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/



More information about the Python-list mailing list