468,288 Members | 1,978 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

Importing at runtime

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
6 1513
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
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
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
"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
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
> 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.

Similar topics

reply views Thread by Mark English | last post: by
12 posts views Thread by qwweeeit | last post: by
2 posts views Thread by Dan | last post: by
reply views Thread by Ivan Lam | last post: by
29 posts views Thread by Natan | last post: by
reply views Thread by google | last post: by
2 posts views Thread by serviceman via AccessMonster.com | last post: by
3 posts views Thread by normanchong | last post: by
3 posts views Thread by abdul | last post: by
1 post views Thread by abhishek | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.