How to launch a script with double-click?

I started to learn Python. For now, I use Udemy lectures.
Currently, I know how to write the simplest scripts without any GUI but still…

How can I start a script like:
print('Hello World')
If I have it in my desktop? (I use XFCE)
I did chmod so it’s executable.

Under right click the first and default option is run.
When I double click (or RMB + run) on the file nothing is going on.
I’ve tried to add #!/bin/python at the beginning of script but that didn’t help either.

And to be clear. I’m not expecting GUI right now. Can it open in terminal window, print Hello World and close?

Here are 3 ways.

All commands are done in the XFCE terminal.

  1. python FILE (where FILE is your python program and there is no shebang line)

  2. To find out the location of the Python Interpreter enter: type -a python
    The shebang line would look like: #!/usr/bin/env python
    See: PEP 394 -- The "python" Command on Unix-Like Systems | Python.org

    If the directory is not in your PATH (echo $PATH), then cd DIR (where DIR is the directory where your file resides) and type ./FILE (where FILE is your python program). Rather than cd you could type the fully-qualified name on the command line ($HOME/DIR/FILE).

  3. You could also work with python interactively by typing python and then at the next prompt enter your print statement. To exit python enter quit() or ctrl-d.

For additional education on the command line, checkout http://www.linuxcommand.org/.
The author has a tutorial on the shell and bash scripting. Both will help with your python.
See: Writing shell scripts - Lesson 1: Writing your first script and getting it to work

2 Likes

I’m not sure but either I don’t understand you or you didn’t understand my question.

Isn’t your advice about running a script from a terminal?
I’m aware that I can run it with command python script.py

I would like to have script.py file on my desktop or any other place and double click on it.
After mouse double click I would like terminal to start and in the terminal there should be script.py started.

I understand English pretty well but I have problem with hypothetical future statements.

It should work like this:

create and save a script hello.py on my desktop
inside script:
#!/usr/bin/env python <- I tried with and without this line. No visible difference.
print('Hello world')
input('Press enter to close')

If necessary chmod a+x ~/Desktop/hello.py
Double click on hello.py
Terminal is open
Hello world is being displayed
Pressing enter - terminal closes

You would need a .desktop file.
https://specifications.freedesktop.org/desktop-entry-spec/latest/

On xfce, right-click the desktop and select “Create Launcher”, enter full path program and select “Run in Terminal”. You might want to add a pause so you see something.

import time
var=input("Hit enter when ready: ")
time.sleep(5)

Do a man xfc4-terminal if you want additional features.

1 Like