How can I make applications opened by subprocess.Popen() stay open after my script exits?

Question:

I want to launch Excel using subprocess.Popen() but my script doesn’t work as expected.

The following simple script doesn’t work in the way I want it to work. It looks like Excel is terminated immediately when the script ends. What do I need to do to keep it open?

import subprocess
subprocess.Popen(r'C:Program Files (x86)Microsoft OfficerootOffice16EXCEL.EXE')

If I enter the same codes into the interactive shell, it works as expected and Excel stays open.

Any help would be greatly appreciated!

Asked By: Zack

||

Answers:

you can use the following

import os
os.system("start excel.exe")
Answered By: Omer Dagry

import subprocess

subprocess.run(r’C:Program FilesMicrosoft OfficerootOffice16EXCEL.EXE’)

Stays Open Until Exit, Then you get this next line…

CompletedProcess(args=’C:Program FilesMicrosoft OfficerootOffice16EXCEL.EXE’, returncode=0)

If you want to do multiple, then multithreading is your answer.

Answered By: Nick Burnette

Thank you everyone for answering my question. I was able to solve it myself. The reason Excel terminates was because I ran the script from VS Code. If I run the script from Command Prompt, Excel stays open.

Answered By: Zack
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.