Home > other >  How do I use python -i 'script.py' without closing terminal window after quitting interpre
How do I use python -i 'script.py' without closing terminal window after quitting interpre

Time:02-11

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. Sorry about my English, no native speaker here.

Thanks!

CodePudding user response:

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?

  • Related