Importing at runtime

Mike Meyer mwm at mired.org
Tue Oct 25 01:25:25 EDT 2005


"David Poundall" <david at jotax.com> writes:
> I have several .py files in a directory that I would like to import at
> run time.  Each file contains a state machine that requires to be run
> in its own thread.

Imports happen at run time. Beware starting threads in the code run at
import time in each module, though - there are some nasty bugs lurking
there. Instead, start the threads in functions invoked from the main
routine.

> The first problem I have is how can I import the code in all of the .py
> files without knowing the file names in advance.
>
> Can this be done ??

Yes. Use the __import__ builtin.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list