-1

I want to open an application with Python, that doesnt close even if the python program stops running... If we take Spotify as an example:

import os

x = "C:\\Users\\49174\\AppData\Roaming\\Spotify\\Spotify.exe"
os.popen(x)
Janne
  • 9
  • 1
  • 3
    I'm afraid you've forgotten to ask a question, you've made a statement. What is the *exact* issue you're facing? – S3DEV Jun 30 '22 at 12:02
  • Use: `os.system(x)` – Maurice Meyer Jun 30 '22 at 12:03
  • Like with popen, the App closes after I stop running my program... But I want it to stay open after closing the program – Janne Jun 30 '22 at 12:09
  • https://stackoverflow.com/questions/11316369/spawning-a-non-child-process-in-python suggests try `subprocess.Popen` – Anentropic Jun 30 '22 at 12:19
  • I don't see any issues with the code you have posted. I've written and tested a similar example to open the calculator, and it remains open after the script closed. I've tested using both a function using `__main__`, and a script as you have posted. – S3DEV Jun 30 '22 at 13:01

2 Answers2

0

I guess you need to pass an additional parameter (creationflags) to Popen. Something like this should work (however, I do not have a Windows machine to check):

import subprocess

creationflags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.subprocess.DETACHED_PROCESS | subprocess.CREATE_NO_WINDOW

x = "C:\\Users\\49174\\AppData\Roaming\\Spotify\\Spotify.exe"
subprocess.Popen(x, creationflags=creationflags)
Yury
  • 20,618
  • 7
  • 58
  • 86
  • This works on Windoze. Strangely enough ... so does the OP's original code. – S3DEV Jun 30 '22 at 15:09
  • I tried it, but as soon as I closed the program, spotify closed as well... – Janne Jun 30 '22 at 20:22
  • I still think that the issue is with the `creationflags`. I've added some more flags that could be useful - see updated code. – Yury Jul 01 '22 at 06:11
0

Try this

import os
os.startfile("C:\\Users\\49174\\AppData\Roaming\\Spotify\\Spotify.exe")
Smaurya
  • 167
  • 9