Dan Bishop wrote: > On Sep 22, 10:09 pm, Connelly Barnes <[EMAIL PROTECTED]> wrote: >> Hi, >> >> I wrote the 'autoimp' module [1], which allows you to import lazy modules: >> >> from autoimp import * (Import lazy wrapper objects around all modules; >> "lazy >> modules" will turn into normal modules when an >> attribute >> is first accessed with getattr()). >> from autoimp import A, B (Import specific lazy module wrapper objects). >> >> The main point of autoimp is to make usage of the interactive Python prompt >> more productive by including "from autoimp import *" in the PYTHONSTARTUP >> file. > > And it does. Gets rid of "oops, I forgot to import that module" > moments without cluttering my $PYTHONSTARTUP file with imports. +1. > > My only complaint is that it breaks globals().
And startup takes quite long the first time, because a list of all available modules must be gathered. To work around that, one can either use a special importing "lib" object, defined like that: class _lib: def __getattr__(self, name): return __import__(name) lib = _lib() or modify the globals() to automatically look up modules on KeyError, like this (put into PYTHONSTARTUP file): import sys, code class LazyImpDict(dict): def __getitem__(self, name): try: return dict.__getitem__(self, name) except KeyError: exc = sys.exc_info() try: return __import__(name) except ImportError: raise exc[0], exc[1], exc[2] d = LazyImpDict() code.interact(banner='', local=d) sys.exit() Of course, this is not perfect as it may break quite a lot of things, I haven't tested it thoroughly. Georg -- http://mail.python.org/mailman/listinfo/python-list