By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

How to execute a python script from another python script?

P: 32
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
Share this Question
Share on Google+
17 Replies


dshimer
Expert 100+
P: 136
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

Expert 100+
P: 511
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

P: 32
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
Expert 5K+
P: 6,596
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

P: 32
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
Expert 5K+
P: 6,596
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

P: 32
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

Expert 100+
P: 511
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
100+
P: 145
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

P: 32
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

P: 32
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
Expert 5K+
P: 6,596
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

P: 32
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
Expert 5K+
P: 6,596
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

P: 1
bro use 'py' in cmd instead of 'python'.if it works or you may need to setup path.
its just an assumption. :))
2 Weeks Ago #18

Post your reply

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