473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

call one python script from within another

What is the best way to call one python script from within another
python script? For example:

one.py finishes succesfully and calls two.py which finishes OK and then
calls three.py

Thanks guys!

Jul 18 '05 #1
3 48756
Tubby Tudor wrote:
What is the best way to call one python script from within another
python script? For example:

one.py finishes succesfully and calls two.py which finishes OK and then
calls three.py

Thanks guys!


one.py:
---------------

import two
import three
def main():
.... body of your code here ....
main() # run code in one
two.main() # run code in two
three.main() # run code in three
------------------

Will this do?

If not, you have to explain better what exactly you want to achieve...

--Irmen

Jul 18 '05 #2
[Tubby Tudor]
What is the best way to call one python script from within another python
script? For example: one.py finishes succesfully and calls two.py which
finishes OK and then calls three.py


I do not know if my way is the best way :-), but the following goes fine for
us. Each of our programs is installed (through `distutils') as a small
bootstrap, on the loader search path, looking like:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
SHORT_DESCRIPTI ON.
"""

import sys
from PACKAGE.MODULE import main
main(*sys.argv[1:])
----------------------------------------------------------------------<

and within the main MODULE for the PACKAGE holding the application, we have:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
LONGER_DESCRITP ION.
"""

[...]

def main(*arguments ):
[...]

if __name__ == '__main__':
main(*sys.argv[1:])
----------------------------------------------------------------------<

We often use this instead, when the complexity warrants it:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
LONGER_DESCRITP ION.
"""

[...]

class Main:
[...]

def main(self, *arguments):
[...]

run = Main()
main = run.main

[...]

if __name__ == '__main__':
main(*sys.argv[1:])
----------------------------------------------------------------------<

So very systematically, one may call any `main' giving, as string arguments,
what would be successive arguments in a shell command calling that program.

When a Python program needs to "execute" another Python program, it often
does something like:

---------------------------------------------------------------------->
[...]
import PACKAGE.MODULE
PACKAGE.MODULE. main(ARGUMENT1, ARGUMENT2, ...)
----------------------------------------------------------------------<

The only reason we would prefer to `exec' or `fork' another Python process
would be to reclaim the storage taken by the imported package modules code,
in the rather unusual cases it would matter. As for the rest of the data,
we merely rely on the garbage collector.

If we were in your situation, it is likely that a small driver would
successively call the three programs as above, one after the other.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #3
Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:<3f******* *************** *@news.xs4all.n l>...
Tubby Tudor wrote:
What is the best way to call one python script from within another
python script? For example:

one.py finishes succesfully and calls two.py which finishes OK and then
calls three.py

Thanks guys!


one.py:
---------------

import two
import three
def main():
.... body of your code here ....
main() # run code in one
two.main() # run code in two
three.main() # run code in three
------------------

Will this do?

Yes, this works. Thanks for the tip!
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1728
by: sarmin kho | last post by:
Hi Python Guru, I ve created a GUI using Boa constructor and am now trying to execute a python script when pressing a button on the GUI. So, there will then be two main threads running (the GUI thread and the python script thread). The purpose of doing this is to enable selecting python script to be run from the GUI. any helps please... many thanks.. sarmin
8
3233
by: Jan Gregor | last post by:
Hello I run python script on another computer and want to "survive" that script after my logout. the script also uses drive mapping to network drive. Can you help me ? Or better is there some info for unix person how to survive with python on windows ;-) thanks, jan gregor
4
4110
by: Christian | last post by:
From a not even newbie: Without knowing much about Python (yet) I'm trying to install the CMS Zope via FTP (with the well documented changes to make it work on an Apache server). By birth Zope is started from a shell script. And not having the permissions to execute such ones I'll try writing a .py script (with the shebang that I allready knows will do the job) to call another .py script like the original shell script does.
2
8964
by: Sibou51 | last post by:
Hello everybody, do you know if it's possible to call a python script in html page?? I want call my script pyhton and I have a html with javascript but I don't know how, do you have an example?? thanks
6
9914
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
17
176694
by: somialis | last post by:
I have a test environment in python. it is executed by the following command on the command prompt. 1) First i have to go to the dir where the .py file is 2) then i simply execute python filename.py and the GUI appears Now i have to create a script which could open the GUI...i.e execute the command "python filename.py" from inside itself.. I tried the "system" command inside my script but the GUI did not appear.i.e i tried system("python...
9
4711
by: Andrew Poulos | last post by:
Say I have this: var foo = function(x, y) { return x + y; }; var bar = function(fname, param1, param2) { /* * how do I call the function "foo" * and pass parameters to it?
1
3580
by: hofsoc20 | last post by:
Hi, I am trying to call a python script (Python 2.5) and implement one of its functions. I have successfully called the script and executed it with jep.runscript("sample.py"). Does anyone know how I can call individual functions within the python script? Thanks in advance...
2
1775
by: gbprithvi | last post by:
if yes..plz give me an example ... thanku :)
2
4745
by: gintrado | last post by:
I am very new to Python I'm trying to run a python script from within another python script. I can run the script from a unix command line by typing: nohup python script.py password > /tmp/outfile.log 2>&1 & Or, I can put that command in a shell script and run it by typing: ./script.sh Both work great. However I don't know how to put either the command or the shell script in a python.py script and get it to run from there. Let me...
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.