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

How to execute a python script from another python script?

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 filename.py") but nothing appeared on the screen

Plz help.
Mar 21 '07 #1
17 176636
dshimer
136 Expert 100+
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.
Mar 21 '07 #2
ghostdog74
511 Expert 256MB
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.
to run another python script from your script:
Expand|Select|Wrap|Line Numbers
  1. import myscript
  2. myscript.somefunc()
  3.  
just like importing the standard modules.
Mar 21 '07 #3
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.


Thanx for the prompt reply...but as u have asked...i had already imported os and executedthe following command from my python script:
os.system("python filename.py")

no error message was flagged and the command was successfully executed leading to the command prompt again but the GUI did not appear.
What could be the probable reason.??
Initially i used to run the GUI by simply typing:
python filename.py
Mar 22 '07 #4
bartonc
6,596 Expert 4TB
Thanx for the prompt reply...but as u have asked...i had already imported os and executedthe following command from my python script:
os.system("python filename.py")

no error message was flagged and the command was successfully executed leading to the command prompt again but the GUI did not appear.
What could be the probable reason.??
Initially i used to run the GUI by simply typing:
python filename.py
Is your GUI Tkinter, or some other toolkit?
Mar 22 '07 #5
Is your GUI Tkinter, or some other toolkit?
i don't know about the tool kit...
it was made for some testing puposes. i load my test files into that and press a runtest button.
Mar 22 '07 #6
bartonc
6,596 Expert 4TB
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 filename.py") but nothing appeared on the screen

Plz help.
By any chance, do you give command line arguments when you call python manually? If so, try
Expand|Select|Wrap|Line Numbers
  1. system("python filename.py arg1 arg2") 
Mar 22 '07 #7
By any chance, do you give command line arguments when you call python manually? If so, try
Expand|Select|Wrap|Line Numbers
  1. system("python filename.py arg1 arg2") 

no there are no command line arguments . i just run python filename.py from the command prompt while running the GUI manually.
Mar 22 '07 #8
ghostdog74
511 Expert 256MB
no there are no command line arguments . i just run python filename.py from the command prompt while running the GUI manually.
personally, i would not recommend this method of calling a python script from anther. The "cleaner" approach is to import it.
for example , in script called myscriptA.py, you have
Expand|Select|Wrap|Line Numbers
  1. def displayGUI():
  2.      print "I am from myscriptA.py in functoin displayGUI"
  3.      return
  4.  
then in myscriptB.py, you can call your GUI display function like this
Expand|Select|Wrap|Line Numbers
  1. import myscriptA ##or from myscriptA import displayGUI
  2. myscriptA.displayGUI()
  3.  
Mar 22 '07 #9
Thekid
145 100+
Hi, I'm just trying to follow along and had a question.....is it that your script will run in a dos window but what you want is for it to run on IDLE?

ex:
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(5):
  2.              print i
  3.  
  4.  
  5. 1
  6. 2
  7. 3
  8. 4
  9. >>>
  10.  
  11.  
so if your saved that script as loop.py:

Expand|Select|Wrap|Line Numbers
  1. for i in range(5):
  2.     print i
  3.  
and then wrote:
Expand|Select|Wrap|Line Numbers
  1. import os,sys
  2. os.system(r"C:\python25\loop.py")
  3.  
then saved it as testrun.py. If you execute testrun.py, it runs the loop in a dos window but you want it to execute and print out to IDLE instead? Is that it, or am I on the wrong track in trying follow?
Mar 22 '07 #10
Hi...
i guess my question is not clear...

i have a test environment. The GUI for the environment appears when on the DOS window i type the following command:

python filename.py

A window appears where in i can load my tests and then click on the tab in the GUI which says run tests.

Now the situation is...i am supposed to do some initial processing before loading the tests.....which includes opening a csv file thru a python script,reading a particular row of the csv file,gettings inputs from there and then loading the test files in the GUI.

so i am supposed to open the GUI after all this processing...from the python script and no more by typing command on the DOS window.

i tried to execute the same command : python filename.py from the python script like this:

import os

os.system("python filename.py")

but it did not wrk...
what shud i do
Mar 23 '07 #11
bartonc
6,596 Expert 4TB
Hi...
i guess my question is not clear...

i have a test environment. The GUI for the environment appears when on the DOS window i type the following command:

python filename.py

A window appears where in i can load my tests and then click on the tab in the GUI which says run tests.

Now the situation is...i am supposed to do some initial processing before loading the tests.....which includes opening a csv file thru a python script,reading a particular row of the csv file,gettings inputs from there and then loading the test files in the GUI.

so i am supposed to open the GUI after all this processing...from the python script and no more by typing command on the DOS window.

i tried to execute the same command : python filename.py from the python script like this:

import os

os.system("python filename.py")

but it did not wrk...
what shud i do
Here is a basic Tkinter GUI probram that doesn't do anything except put a button in a frame:
Expand|Select|Wrap|Line Numbers
  1. # TkButtonFrame.py'
  2. from Tkinter import *
  3.  
  4. class TestFrame(Frame):
  5.     def __init__(self, parent, *args, **kwargs):
  6.         Frame.__init__(self, parent)
  7.         self.testButton = Button(text='Run Test')
  8.         self.testButton.pack()
  9.  
  10.  
  11.  
  12. if __name__ == "__main__":
  13.     root = Tk()
  14.     testFrame = TestFrame(root)
  15.     testFrame.pack()
  16.     root.mainloop()
  17.  
It does have the __main__ "guard" in it that I thought might cause the problem.

Here is the command module:
Expand|Select|Wrap|Line Numbers
  1. # rungui.py
  2. import os
  3. os.system('python TkButtonFrame.py')
  4.  
If you run the command rungui.py, the GUI will appear.
If it looks like your test program, then the toolkit used to make it is Tkinter.
If you put these modules into your test environment and they don't work, then something (most likely PYTHONPATH) is not set up correctly.
Mar 23 '07 #12
bartonc
6,596 Expert 4TB
Here is a basic Tkinter GUI probram that doesn't do anything except put a button in a frame:
Expand|Select|Wrap|Line Numbers
  1. # TkButtonFrame.py'
  2. from Tkinter import *
  3.  
  4. class TestFrame(Frame):
  5.     def __init__(self, parent, *args, **kwargs):
  6.         Frame.__init__(self, parent)
  7.         self.testButton = Button(text='Run Test')
  8.         self.testButton.pack()
  9.  
  10.  
  11.  
  12. if __name__ == "__main__":
  13.     root = Tk()
  14.     testFrame = TestFrame(root)
  15.     testFrame.pack()
  16.     root.mainloop()
  17.  
It does have the __main__ "guard" in it that I thought might cause the problem.

Here is the command module:
Expand|Select|Wrap|Line Numbers
  1. # rungui.py
  2. import os
  3. os.system('python TkButtonFrame.py')
  4.  
If you run the command rungui.py, the GUI will appear.
If it looks like your test program, then the toolkit used to make it is Tkinter.
If you put these modules into your test environment and they don't work, then something (most likely PYTHONPATH) is not set up correctly.
It's important to note that
Expand|Select|Wrap|Line Numbers
  1. import TkButtonFrame
WILL NOT WORK because of the if __name__ == "__main__": guard.
Mar 23 '07 #13
Here is a basic Tkinter GUI probram that doesn't do anything except put a button in a frame:
Expand|Select|Wrap|Line Numbers
  1. # TkButtonFrame.py'
  2. from Tkinter import *
  3.  
  4. class TestFrame(Frame):
  5.     def __init__(self, parent, *args, **kwargs):
  6.         Frame.__init__(self, parent)
  7.         self.testButton = Button(text='Run Test')
  8.         self.testButton.pack()
  9.  
  10.  
  11.  
  12. if __name__ == "__main__":
  13.     root = Tk()
  14.     testFrame = TestFrame(root)
  15.     testFrame.pack()
  16.     root.mainloop()
  17.  
It does have the __main__ "guard" in it that I thought might cause the problem.

Here is the command module:
Expand|Select|Wrap|Line Numbers
  1. # rungui.py
  2. import os
  3. os.system('python TkButtonFrame.py')
  4.  
If you run the command rungui.py, the GUI will appear.
If it looks like your test program, then the toolkit used to make it is Tkinter.
If you put these modules into your test environment and they don't work, then something (most likely PYTHONPATH) is not set up correctly.


The __main__ of my test file looks like this:
Expand|Select|Wrap|Line Numbers
  1. # if running standalone
  2. if __name__ == "__main__":
  3.     app = MyApp(0)  # Create an instance of the application class
  4.     app.MainLoop()  # Tell it to start processing events
  5.  
  6. # End of Program
Mar 23 '07 #14
bartonc
6,596 Expert 4TB
The __main__ of my test file looks like this:
Expand|Select|Wrap|Line Numbers
  1. # if running standalone
  2. if __name__ == "__main__":
  3.     app = MyApp(0)  # Create an instance of the application class
  4.     app.MainLoop()  # Tell it to start processing events
  5.  
  6. # End of Program
I've added CODE tags to your post. Please read "REPLY GUIDELINES" to learn how to do this.

So how is the class MyApp defined?
Mar 23 '07 #15
I've added CODE tags to your post. Please read "REPLY GUIDELINES" to learn how to do this.

So how is the class MyApp defined?

Class MyaApp is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class MyApp(wx.App):
  3.     # wxWindows calls this method to initialize the application
  4.     def OnInit(self, operator=None):
  5.  
  6.         # Create an instance of our customized Frame class
  7.         frame = MyFrame(None, -1, "PetShopTest")
  8.         frame.Show(1)
  9.         # and let wxWindows know that is our main window...
  10.         self.SetTopWindow(frame)
  11.  
  12.         # we need to return a success flag (to keep wxWindows happy)...
  13.         return 1
  14.  
  15.  
Mar 23 '07 #16
bartonc
6,596 Expert 4TB
Class MyaApp is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class MyApp(wx.App):
  3.     # wxWindows calls this method to initialize the application
  4.     def OnInit(self, operator=None):
  5.  
  6.         # Create an instance of our customized Frame class
  7.         frame = MyFrame(None, -1, "PetShopTest")
  8.         frame.Show(1)
  9.         # and let wxWindows know that is our main window...
  10.         self.SetTopWindow(frame)
  11.  
  12.         # we need to return a success flag (to keep wxWindows happy)...
  13.         return 1
  14.  
  15.  
Awesome! Your GUI Toolkit is wxPython. It's the best in my opinion.
Since this should work, there can be only one reson why it doesn't:
Python doesn't know how to find your program (you've been calling it "filename.py). You probably need to specify the entire path in the command. Use raw strings for path names on windows:
Expand|Select|Wrap|Line Numbers
  1. r"D:\Python24\Lib\filename.py"
Or you can add the path to the environment variable called PYTHONPATH in the operating system. Or you can put path into a file with any name (say) testpath.pth into the D:\Python24\Lib\site-packages (on my system) directory. The .pth extention tells python to add whaterver is in that file to sys.path.
Mar 23 '07 #17
bro use 'py' in cmd instead of 'python'.if it works or you may need to setup path.
its just an assumption. :))
Nov 22 '19 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Amir | last post by:
Can one PHP script execute another PHP script?
0
by: Irmen de Jong | last post by:
QOTW: "Confronting the Martellibot is like flirting with an encyclopedia, I'd rather not do it myself, but I respect those who do, because it produces knowledge." -- Anton...
1
by: Steve | last post by:
Hi, I've written a small cgi python script that collects feedback from a webform. I need to pass the values to another python script.. how do I do this? Is this even possible? Thanks Steve
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
8
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am...
2
by: KraftDiner | last post by:
I have a class that is defined in a file called MyClass.py How do I use that class in another python script.. import MyClass ? (Does it need to be in a specific location?)
2
by: maya | last post by:
hi, I keep getting this error, "can't execute code from a freed script" when I go to another page (i.e., upon UNLOAD..) this error occurs only in IE, not FF (what a surprise.....;) what is...
2
by: Alexandru Mosoi | last post by:
how do I execute another python script under a different process? I want the script to be run using the same interpretoer as the one running current script. I tried using os.execlp but I don't know...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.