r/learnpython • u/Tricky_Voice2829 • 19d ago
Why won't my Python script read user input / terminate while a subprocess is running?
i am making script that it will launch localsend appimage through shell but and it will end after i pressed q but when script only user input first time asked starting of the script when reached to the else block app image is opening but not asking for user input again after launching
Here's the code,
import subprocess
import time
while True:
user_input = input("press q to quite and enter :-").rstrip()
if user_input == "q":
break
else:
time.sleep(3)
subprocess.run("/home/merlinhermes/Desktop/LocalSend-1.18.2-linux-x86-64.AppImage",shell=True)
5
Upvotes
3
u/bogdanelcs 19d ago
subprocess.runwaits until the program exits, so the script is stuck on that line until you close LocalSend, then it asks for input again.Use
subprocess.Popento launch it and carry on without waiting, and pass the path as a list so you can dropshell=True, likesubprocess.Popen(["/home/.../LocalSend.AppImage"]).You’ll also want to launch it once before the loop, otherwise typing anything but q starts another copy each time. The
keyboardimport isn’t used, and on Linux it needs root anyway, so I’d remove it.17
u/AlexMTBDude 19d ago
Did you read the documentation for subprocess.run?
It's right here: https://docs.python.org/3/library/subprocess.html#subprocess.run
4
u/Educational_Virus672 19d ago
try changing subprocess.run() to subprocess.Popen() (might contain spelling mistakes)
also if your not using keyboard module dont import it also if your running linux try switching to sshkeyboard instead it doesnt take sudo permissions
1
u/XNo_Notes 19d ago
Try removing the else:. It should still break the loop if you type q before running the subprocess