2

I am aware that I can run a Python script on Linux.

  1. From the terminal by ./hello_world.py.

  2. 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?

3
  • 2
    Is this not simply a case of the terminal window flashing open and closing after the script runs? Perhaps put a sleep timer in the script to pause, enabling you to read the output, before the terminal window closes. Commented May 23, 2020 at 18:03
  • @S3DEV I briefly had the same thought, but I can see no window flashing open, and including sleeps both at the beginning and at the end of the script didn't help. Commented May 23, 2020 at 18:08
  • Interesting. I’ve just tried (using Mint), and when I click to run the .py file from the file manager I’m prompted to select ‘Run in Terminal’ which opens a terminal and displays the output (if sleep is implemented) and ‘Run’ which just runs in the background. TBH: I never use the file manager, my life is in Terminal. So will leave for others to answer. Sorry. Commented May 23, 2020 at 18:10

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This is a nice idea; unfortunately this will only let the console window flash up for the blink of an eye, then close it again. Is there a way to tell the script to leave the console open until after execution? (A sleep statement after the konsole command doesn't do the trick. I could insert the respective sleep statements in each of my Python scripts, but it would be more elegant and practical to have a generic solution.)
My thoughts on "generic" being that one could abstract this so as to take a file name as an argument, which one could then e.g. register as the default application to open *.py-files with, so the wrapper script will work automatically on any python file rather than having to be manually set up for each python file separately. I just tried this out and it worked, except for the window closing again issue.
@lemontree, add --noclose option to prevent automatic window close
This works; thank you. I upvoted your answer, but am going with my extension as the final solution.
0

I decided for the following generic solution based on Aleksey's answer:

  1. 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.

  2. Make this file executable.

  3. 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.

  4. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.