By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

Importing at runtime

P: n/a
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.

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 ??

TIA

Oct 25 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
exec "import something"

On Monday 24 October 2005 21:53, David Poundall wrote:
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.

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 ??

TIA


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 25 '05 #2

P: n/a
Thanks James, just after I posted I stumbled across the execfile
command. Looks like with 'exec' and 'execfile' I should be able to do
what I want.

What an elegant language.

Oct 25 '05 #3

P: n/a
http://www.python.org/doc/2.4.2/lib/built-in-funcs.html

or, if you want an answer in code now and don't want to read the docs

def my_import(name):
module = __import__(name)
globals()[name] = module #not a good idea

Or, seeing as how you won't be directly accessing them by name, anyways

modules = [__import__(name) for name in module_names]

Oct 25 '05 #4

P: n/a
"David Poundall" <da***@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 25 '05 #5

P: n/a
Devan, would ...

__import__(name) for name in module_names

have worked just as well - without the equate to modules? or is the
modules list required as a hook for the imports ?

Oct 25 '05 #6

P: n/a
> 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.


I have run into trouble trying to do that before. Thanks for the
reminder Mike.

Oct 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.