How to execute a python script from another python script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somialis
    New Member
    • Mar 2007
    • 32

    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.
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    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
    Code:
    import os
    os.system('python hello.py')
    and hello.py just prints hello, then
    Code:
    C:\tmp>python test.py
    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.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by dshimer
      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
      Code:
      import os
      os.system('python hello.py')
      and hello.py just prints hello, then
      Code:
      C:\tmp>python test.py
      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:
      Code:
      import myscript
      myscript.somefunc()
      just like importing the standard modules.

      Comment

      • somialis
        New Member
        • Mar 2007
        • 32

        #4
        Originally posted by dshimer
        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
        Code:
        import os
        os.system('python hello.py')
        and hello.py just prints hello, then
        Code:
        C:\tmp>python test.py
        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("pyth on 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

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by somialis
          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("pyth on 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?

          Comment

          • somialis
            New Member
            • Mar 2007
            • 32

            #6
            Originally posted by bartonc
            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.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by somialis
              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
              Code:
              system("python filename.py arg1 arg2")

              Comment

              • somialis
                New Member
                • Mar 2007
                • 32

                #8
                Originally posted by bartonc
                By any chance, do you give command line arguments when you call python manually? If so, try
                Code:
                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.

                Comment

                • ghostdog74
                  Recognized Expert Contributor
                  • Apr 2006
                  • 511

                  #9
                  Originally posted by somialis
                  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
                  Code:
                  def displayGUI():
                       print "I am from myscriptA.py in functoin displayGUI"
                       return
                  then in myscriptB.py, you can call your GUI display function like this
                  Code:
                  import myscriptA ##or from myscriptA import displayGUI
                  myscriptA.displayGUI()

                  Comment

                  • Thekid
                    New Member
                    • Feb 2007
                    • 145

                    #10
                    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:
                    Code:
                    >>> for i in range(5):
                                 print i
                    
                    0
                    1
                    2
                    3
                    4
                    >>>
                    so if your saved that script as loop.py:

                    Code:
                    for i in range(5):
                        print i
                    and then wrote:
                    Code:
                    import os,sys
                    os.system(r"C:\python25\loop.py")
                    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?

                    Comment

                    • somialis
                      New Member
                      • Mar 2007
                      • 32

                      #11
                      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...fr om 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("pyth on filename.py")

                      but it did not wrk...
                      what shud i do

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by somialis
                        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...fr om 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("pyth on 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:
                        Code:
                        # TkButtonFrame.py'
                        from Tkinter import *
                        
                        class TestFrame(Frame):
                            def __init__(self, parent, *args, **kwargs):
                                Frame.__init__(self, parent)
                                self.testButton = Button(text='Run Test')
                                self.testButton.pack()
                        
                        
                        
                        if __name__ == "__main__":
                            root = Tk()
                            testFrame = TestFrame(root)
                            testFrame.pack()
                            root.mainloop()
                        It does have the __main__ "guard" in it that I thought might cause the problem.

                        Here is the command module:
                        Code:
                        # rungui.py
                        import os
                        os.system('python TkButtonFrame.py')
                        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.

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by bartonc
                          Here is a basic Tkinter GUI probram that doesn't do anything except put a button in a frame:
                          Code:
                          # TkButtonFrame.py'
                          from Tkinter import *
                          
                          class TestFrame(Frame):
                              def __init__(self, parent, *args, **kwargs):
                                  Frame.__init__(self, parent)
                                  self.testButton = Button(text='Run Test')
                                  self.testButton.pack()
                          
                          
                          
                          if __name__ == "__main__":
                              root = Tk()
                              testFrame = TestFrame(root)
                              testFrame.pack()
                              root.mainloop()
                          It does have the __main__ "guard" in it that I thought might cause the problem.

                          Here is the command module:
                          Code:
                          # rungui.py
                          import os
                          os.system('python TkButtonFrame.py')
                          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
                          Code:
                          import TkButtonFrame
                          WILL NOT WORK because of the if __name__ == "__main__": guard.

                          Comment

                          • somialis
                            New Member
                            • Mar 2007
                            • 32

                            #14
                            Originally posted by bartonc
                            Here is a basic Tkinter GUI probram that doesn't do anything except put a button in a frame:
                            Code:
                            # TkButtonFrame.py'
                            from Tkinter import *
                            
                            class TestFrame(Frame):
                                def __init__(self, parent, *args, **kwargs):
                                    Frame.__init__(self, parent)
                                    self.testButton = Button(text='Run Test')
                                    self.testButton.pack()
                            
                            
                            
                            if __name__ == "__main__":
                                root = Tk()
                                testFrame = TestFrame(root)
                                testFrame.pack()
                                root.mainloop()
                            It does have the __main__ "guard" in it that I thought might cause the problem.

                            Here is the command module:
                            Code:
                            # rungui.py
                            import os
                            os.system('python TkButtonFrame.py')
                            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:
                            Code:
                            # if running standalone
                            if __name__ == "__main__":
                                app = MyApp(0)  # Create an instance of the application class
                                app.MainLoop()  # Tell it to start processing events
                                
                            # End of Program
                            Last edited by bartonc; Mar 23 '07, 05:48 AM. Reason: added [code][/code] tags

                            Comment

                            • bartonc
                              Recognized Expert Expert
                              • Sep 2006
                              • 6478

                              #15
                              Originally posted by somialis
                              The __main__ of my test file looks like this:
                              Code:
                              # if running standalone
                              if __name__ == "__main__":
                                  app = MyApp(0)  # Create an instance of the application class
                                  app.MainLoop()  # Tell it to start processing events
                                  
                              # 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?

                              Comment

                              Working...