Automatic Python imports.
At the interactive Python prompt, I found myself wasting a significant
amount of time by repeatedly writing statements such as
import urllib2, os, socket. Every time that I opened a new Python
session, I needed to re-import all of the libraries that I intended to
autoimp module imports all available Python modules
automatically, similarly to Matlab:
>>> from autoimp import *
The modules imported from
autoimp are proxy objects which
lazily load when they are first used. To properly use
autoimp, you should place the following code in your
from autoimp import *
Now, all modules are automatically imported and made available in your
interactive Python sessions. For IPython, you can instead add the following
c.InteractiveShellApp.code_to_run = 'from autoimp import *'
If you are using an editor that doesn't support either of these startup options, see note .
- Public domain, compatible with Python 2.1 - 2.7, PyPy, IPython, Cython.
- Can be used in Python source code files.
- Tested with the Python standard library and extensions: CGKit, Numpy, Scipy, OpenGL, PIL, Pygame, and ODE.
- Works with documentation utilities such as pydoc and epydoc if
__all__ is defined.
- Does not currently work with py2exe and pyinstaller, because the installers cannot determine which modules were imported.
- Limitation: In directories with tens of thousands or more files, Python sessions will be very slow to
start due to the auto import checking all files. As a hack for now I just don't start interactive sessions in
directories with many files.
Version 1.0.2 source code is available.
Version history (changelog):
The autoimp source code and supporting documentation have
been placed in the public domain.
Please send patches and bug reports to my e-mail address:
- Connelly Barnes
||Alternatively, for editors such as IDLE
which do not load the |
PYTHONSTARTUP file after running a
script, you can (a) patch your editor or (b) use a hack to circumvent
the problem: add the following code to
import autoimp; autoimp._export_builtins()
This hack will cause all available modules to be placed in the builtin
namespace of every Python script which is run; this can presumably
cause hard-to-find bugs. You have been warned.