r/learnpython • • Oct 29 '26

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

14 comments sorted by

3

u/bogdanelcs Oct 29 '26

subprocess.run waits until the program exits, so the script is stuck on that line until you close LocalSend, then it asks for input again.

Use subprocess.Popen to launch it and carry on without waiting, and pass the path as a list so you can drop shell=True, like subprocess.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 keyboard import isn’t used, and on Linux it needs root anyway, so I’d remove it.

1

u/Tricky_Voice2829 Oct 29 '26

ok thatnk you

1

u/Forward-Ride2950 Oct 29 '26

If you don't mind would you tell why is appimage needs to put withing square bracket  ["/home/.../LocalSend.AppImage"]

1

u/Lilinxi159 Oct 29 '26

The brackets make it a list, and a list is how you hand subprocess the program and its arguments without going through a shell. The first item is the program, every item after it is one argument, passed through exactly as written.

With a plain string and no shell=True, Python on Linux treats the whole string as the name of the program. That happens to work here because there are no arguments, but something like "LocalSend.AppImage --help" would fail, since it looks for a file literally called that.

shell=True with a string works too, but then /bin/sh parses the line, so spaces in paths, quotes and special characters all start to matter. The list form skips all of that, which is why it's usually the safer default. The subprocess docs spell this out under "Frequently Used Arguments".

17

u/AlexMTBDude Oct 29 '26

Did you read the documentation for subprocess.run?

Wait for command to complete, then return a CompletedProcess instance.

It's right here: https://docs.python.org/3/library/subprocess.html#subprocess.run

1

u/tambooby Oct 29 '26

this is exactly it, the blocking behavior is the whole issue here

4

u/Educational_Virus672 Oct 29 '26

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

7

u/mjmvideos Oct 29 '26

“you are” contracts to you’re. “your” is a possessive: “Is this your book or is it mine?”

2

u/gdchinacat Oct 30 '26

example of you're: "you're being pedantic". (don't take this the wrong way, I encourage pedantry).

2

u/mjmvideos Oct 30 '26

I see enough people use these incorrectly to wonder if they just don’t know or if they just don’t care. Especially in programming groups, if I wrote:

fer i in range(10) 

Or

wile (x < 10) 

Your program wouldn’t run, even though “you knew what I meant”. People take care to use proper syntax and grammar when they have to. English is a language too. I’d just like to see it being used properly. Maybe I should respond with:

Syntax error on line 2: unexpected “your”. Did you mean “you’re”?

1

u/Educational_Virus672 Oct 30 '26

if it doesnt affect readbility then it is unnecessary im not trying to write any former stuff here, am i?

2

u/mjmvideos Oct 30 '26

Yore write. It dozen effect reed a bill a tee. Ewe shed jess right hoe heifer u chews.

1

u/Educational_Virus672 29d ago

sure buddy sure but we are not the same you just type a fake english sentence yet much of it is understandable also im not gonna take time for every comment. every time i polish here is how much i missed somewhere else im not taking time for a thing that saves 3 seconds of others

1

u/XNo_Notes Oct 29 '26

Try removing the else:. It should still break the loop if you type q before running the subprocess