473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1607
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
2
by: Dan | last post by:
I am attempting to use a function written in C and complied to a C dll in my C# project. The function I am attempting to call returns an integer value and takes, as an input parameter, a value of...
0
by: Ivan Lam | last post by:
Hi All, Thanks for reading my question. I am a newbie of VC .Net, and I am facing a problem about importing DLL. I have searched from the web and I download a simple sample. And the code...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
0
by: google | last post by:
Hi, I currently have a .net page default.aspx - as so: <%@ Page Inherits="webmail.telnet_test" Src="./mail_code/telnet.cs" Trace="true" Language="C#" %> <html> <head>
2
by: serviceman via AccessMonster.com | last post by:
Me Again, I just imported a working project from an SQL server based setup to an Access based setup, and now this code is giving me a 'runtime error 438 not defined' message : Private Sub...
3
by: normanchong | last post by:
Hi, I'm currently developing an application for checking and doing some configurations for another application. During those checks, I have to access SQLServer to get configuration-info like...
3
by: abdul | last post by:
I have imported C++ dll in C#. But i am getting this error. "An unhandled exception of type 'System.EntryPointNotFoundException' occurred in ConsoleApplication.exe Additional information: Unable to...
1
by: abhishek | last post by:
hello group , I have build a python c extension. Using python 2.5 , VS.Net 2005 on Win server 2003. But when i am trying to imort this .pyd file into python interperter or my project source...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.