Reputation: 494
I am aware that I can run a Python script on Linux.
From the terminal by ./hello_world.py
.
From the file manager by clicking on the file.
... after including the shebang and marking the file as executable.
However, option 1 requires me to manually open a terminal and type out the file name, and option 2 won't show me any output from the script such as print statements and error messages since no output window will be opened, the script will just run invisibly in the background.
How can I configure my script file or Linux (Kubuntu 20.04 in my case) setup such that starting a *.py file from the file manager will automatically open it in a terminal window so that it shows the program output?
Upvotes: 2
Views: 490
Reputation: 494
I decided for the following generic solution based on Aleksey's answer:
Create a file (e.g. as /home/lemontree/run_python.sh
) with content
#!/bin/sh
konsole --noclose -e python3 $1
This is for KDE's default terminal application Konsole; if you would like to use a different terminal application, the command and options will have to be adapted accordingly.
Make this file executable.
Register this wrapper script as the default application to open
*.py
files with.
In KDE, this is in System
settings/Applications/File associations. Search for "python",
and under Application preference order with Add enter the path
to your wrapper script. Make sure your script is on top of the application preference order list.
Note that when clicking your python file, you now must select to open rather than execute it. Opening the file in a text editor can still be achieved via the context menu.
Clicking on any .py
file will now execute it in a terminal window.
Upvotes: 0
Reputation: 803
You can create simple Bash wrapper script with start of your terminal app.
Here is example for QTerminal
#!/bin/sh
qterminal -e python3 ~/software/myscript.py
Kubuntu default terminal app is Konsole. And it has -e option too: Command-line Options.
You can also add --noclose option to prevent auto close window.
So, your Bash wrapper script will look like this:
#!/bin/sh
konsole --noclose -e python3 ~/software/myscript.py
Add execution rights, and you will be able run your Python script in terminal window by doubleclicking on this wrapper script.
Upvotes: 3