1

How do I use python -i 'script.py' without closing terminal window after quitting the script in interactive mode on Linux?

I want to return to terminal bash environment without closing the terminal window when I just quit the interpreter environment.

I use the following command inside a .desktop file in exec= session to launch the python script by an icon or shortcut.

gnome-terminal --full-screen -- python3 -i "path/to/script.py"
starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Python does not quit the interactive interpreter unless your script calls the built-in `exit()` or `sys.exit()`, which it should probably not. – Richard Neumann Feb 09 '22 at 15:04
  • Yes, you are right! What I am asking is a different question as a matter of fact, I want to return to terminal bash environment without closing the window when I just quit the interpreter environment. Sorry about my English, no native speaker here. – Luiz Guilherme Feb 09 '22 at 19:49
  • If you launch the Python process from an interactive shell, python should have no means, besides invoking *kill* to close the parent shell session. Please provide detailed steps how you start the terminal session and the python script. – Richard Neumann Feb 09 '22 at 22:14
  • Sorry, I did not mention a important detail, because the command that follows, **gnome-terminal --full-screen -- python3 -i "path/to/script.py"**, is inside a .desktop file in exec= session to launch the python script by an icon or shortcut. – Luiz Guilherme Feb 10 '22 at 10:08

1 Answers1

0

As per your comment, you launch the gnome-terminal with python as the main process. Per default, gnome-terminal closes itself, if the shell process, in your case python, exits. You have two options.

Modify the beahviour of gnome-terminal

In the settings for gnome-terminal navigate to your used profile (left sidebar), then to the 4th tab (named something like Command). In the bottom drowpdown menu (named something like When command exits) you can set gnome-terminal to keep running when the command exits.

However, this is most likely not what you want, since you'll be left with a non-functional terminal window without a running shell.

Wrap your command in a shell process

If you want an interactive shell after python exits, you need to start one in the first place. To make it fall back to a shell, you can tell it to execute the shell again, after python exits:

gnome-terminal --full-screen -- /bin/bash -c "python3 -i path/to/script.py; bash"

See also: How to invoke bash, run commands inside the new shell, and then give control back to user?

Richard Neumann
  • 2,986
  • 2
  • 25
  • 50