Importing Modules

Terry Hancock hancock at
Wed Nov 2 18:49:33 CET 2005

On Wednesday 02 November 2005 07:23 am, Peter Hansen wrote:
> Walter Brunswick wrote:
> > The purpose is rather irrelevant. 
> The purpose of something is not only relevant but essential when someone 
> asks for the "best" way to do it.
> For example, without knowing anything of your purpose, I could simply 
> say that writing a module with a series of import statements, one per 
> module in the target subdirectory, is the "best" way, and I could easily 
> defend that as "best" against all other possible approaches until we 
> knew what the real reason for doing this was.

Indeed for any situation in which this solution is possible, I would
regard it as the best way to import modules: "explicitly".

The need to import modules implicitly usually implies some kind of
plugin pattern. A simple application of os.listdir(), some use of
glob or simple string methods, and the __import__ built-in function 
will usually do what you want in the implicit case.

But don't do the implicit version unless you have a very good
reason for doing so (such as a plugin model where the code will
be maintained by someone other than the author of the main program).
In such a situation you should also take reasonable precautions to
prevent major collisions, even if you expect it to be the plugin
author's responsibility to avoid them (e.g. establish interface
rules for plugins and a formal API for interacting with your

Terry Hancock ( hancock at )
Anansi Spaceworks

More information about the Python-list mailing list