4

I would like to launch a python program from another python program and concurrently quit the previous program.

Here what i've done:

if(self.start_button.pression==1):
        os.system('python program_to_launch.py')
        sys.exit()

The result is quite obvious, the program launch the 'program_to_launch.py' and wait until it finish to run.

The problem lies in the while loop, inside the just launched program. It's made to last for an indefinite amount of time.

Here's the problem; the previous program is still waiting the ending of the new program, and it will not execute the` sys.exit()

I could kill the first program using`

NOTES:

Following the method provided by this answer, i can't import the program to launch because i want to terminate the first program

import program_to_launch 

So i think that it isn't a solution. And the command exec file()

execfile('program_to_launch.py')

as suggested in the linked answer, it provide problems to import several modules in the execution of program_to_launch.py

3 Answers 3

4

Lets say I have the following file (named maintenance.py) that I want to run and immediately after doing so, end my application:

import wx

myApp = wx.App()
dial = wx.MessageDialog(None, f"This program is temporarily down for maintenance.\nWe will do our best to get it running again.\nThank you for your patience.", "Error", wx.OK|wx.ICON_ERROR)
dial.ShowModal()

Use the Subprocess Module

This would be the preferred method. Just call it using the Popen command.

import sys
import subprocess
subprocess.Popen(["py", "maintenance.py"])
sys.exit()

For more information, see: https://docs.python.org/3.6/library/subprocess.html#popen-constructor

Use a Shortcut

If subprocess is not working for you, you could create a shortcut and launch the shortcut using os.startfile. For example, a shortcut named myShortcut.lnk that has the following for a target: C:\Windows\py.exe C:\Users\kblad\Desktop\maintenance.py will work for the following code:

import os
import sys
os.startfile("myShortcut")
sys.exit()

For more information, see: https://docs.python.org/3.6/library/os.html#os.startfile

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This fixed a lot of problems. I do not know what the list syntax is for but I just used subprocess.Popen(path) and it worked beautifully.
You can provide a list of what you would type in the cmd window or just a string. The linked docs in the above answer explain it more in detail. In the end though, just use whatever makes more sense to you. :)
@Ruben List syntax separates arguments. For example, "python \"some program.py\"' is not necessary because you do ["python", "some program.py"]. In the case of Kade's answer, "py" is the Windows Python runner often installed with Python. If you need a specific version, you can add "-2" or "-3" like: ["py", "-3", "maintenance.py"] for Python 3 but only if using "py". If didn't do "Add to PATH" during Python install, use full path to py.exe. Anything after the 1st element in the list is a command argument for the previous. In the case of py files, ones following the py file are for the script.
0

I think you can just launch the program in a new thread or process:

from os import system
from threading import Thread
from sys import exit

if self.start_button.pression == 1:
    thread = Thread()
    thread.run = lambda: system('python program_to_launch.py')
    thread.start()

    exit()

5 Comments

Unfortunately the previous program remain open and freezed, i used the function print('bookmark') to see where the program arrive in the execution, it arrive before the exit() function. At least when i close the second program the first program closes it too.
Instead of os.system try subprocess.Popen. It should work even without the threading
p2 = subprocess.Popen("/Users/johndoe/Desktop /PythonEXp/program_to_launch.py") In this way i've found a lot problems, first i have to specify the path (it's not a problem itself, but the code became less dynamic) and second it gives me some problem about the authorization. OSError: [Errno 13] Permission denied
Note, i've also tried p2 = subprocess.Popen("program_to_launch.py -p") but it doesn't find the program
Popen('C:/Python36-32/python.exe "D:/Making/Python/random scripts/conways_game_short_glider.py"') This worked for me. I used an absolute path. There are also already questions about this: stackoverflow.com/q/21412610/7062162
0

I was looking for a way to reload my own python script, and I came across this article: https://blog.petrzemek.net/2014/03/23/restarting-a-python-script-within-itself/

I didn't quite understand where the ['python'] thing came from, but after reading the python documentation for OS.exec*, I came across this:

the first of these arguments is passed to the new program as its own name rather than as an argument a user may have typed on a command line. For the C programmer, this is the argv[0] passed to a program’s main(). For example, os.execv('/bin/echo', ['foo', 'bar']) will only print bar on standard output; foo will seem to be ignored.

https://docs.python.org/2/library/os.html Section 15.1.5

This might work for you as it seems to replace the original process?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.