473,418 Members | 1,767 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,418 software developers and data experts.

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 48746
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_DESCRIPTION.
"""

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_DESCRITPION.
"""

[...]

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_DESCRITPION.
"""

[...]

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.nl>...
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
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...
8
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...
4
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...
2
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...
6
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
17
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...
9
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
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...
2
by: gbprithvi | last post by:
if yes..plz give me an example ... thanku :)
2
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 >...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.