CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

How to run the Python script with a single click?

Fri Aug 04, 2017 6:24 pm

To run a program written in Python, I must first open the file, and then click on the "Run" button. Can I run it with one click?

User avatar
DougieLawson
Posts: 35805
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to run the Python script with a single click?

Fri Aug 04, 2017 7:40 pm

If you create an *.desktop file you can make it a stand-alone icon that runs on a click.

https://linuxcritic.wordpress.com/2010/ ... ktop-file/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 10:06 am

DougieLawson wrote:
Fri Aug 04, 2017 7:40 pm
If you create an *.desktop file you can make it a stand-alone icon that runs on a click.

https://linuxcritic.wordpress.com/2010/ ... ktop-file/
I did as I said in this source, but when I launch the shortcut, I get an error: "Invalid label file" /home/pi/.local/share/applications/cam.desktop ".

User avatar
PeterO
Posts: 4942
Joined: Sun Jul 22, 2012 4:14 pm

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 10:17 am

CAP33 wrote:
Sat Aug 05, 2017 10:06 am
I did as I said in this source, but when I launch the shortcut, I get an error: "Invalid label file" /home/pi/.local/share/applications/cam.desktop ".
What did you put in that file ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 10:42 am

PeterO wrote:
Sat Aug 05, 2017 10:17 am
What did you put in that file ?
PeterO

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=Cam
Type=link
Terminal=folse
Exec=/home/pi/Desktop/Python/Pi_Camera/Cam.py

User avatar
B.Goode
Posts: 8258
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 10:49 am

Code: Select all

Terminal=folse
Exec=/home/pi/Desktop/Python/Pi_Camera/Cam.py
Is folse a valid keyword?

Is /home/pi/Desktop/Python/Pi_Camera/Cam.py executable? Does it start with a #! line that invokes the Python interpreter?

Edit: added later - although it probably won't cause the error you have reported, is

Code: Select all

Type=link
appropriate? Maybe Application is what you need here?

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 12:50 pm

B.Goode wrote:
Sat Aug 05, 2017 10:49 am
Is folse a valid keyword?
Is /home/pi/Desktop/Python/Pi_Camera/Cam.py executable? Does it start with a #! line that invokes the Python interpreter?
Edit: added later - although it probably won't cause the error you have reported, is

Code: Select all

Type=link
appropriate? Maybe Application is what you need here?
Yes, /home/pi/Desktop/Python/Pi_Camera/Cam.py is an executable file.
Terminal=folse This line is in almost all similar files. But I do not know if she really needs to be here.
I do not really understand you at the expense of the sign "#". Where should it be placed? After all, any line starting with "#" is a comment, is not it?

User avatar
PeterO
Posts: 4942
Joined: Sun Jul 22, 2012 4:14 pm

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 1:28 pm

Terminal=false
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
B.Goode
Posts: 8258
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 1:56 pm

Terminal=folse This line is in almost all similar files. But I do not know if she really needs to be here.
In the documentation you were referred to it is described as an Optional Element. So it does not have to be there. But if you are going to include it, it has to be syntactically correct, as has been pointed out to you.
any line starting with "#" is a comment, is not it?
No.

That is not totally true.

An initial line beginning #! is used to instruct the system how to process the remainder of the file. (A .py filename extension is not enough to tell the system to treat the file as a python script.)

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 2:10 pm

B.Goode wrote:
Sat Aug 05, 2017 1:56 pm
An initial line beginning #! is used to instruct the system how to process the remainder of the file. (A .py filename extension is not enough to tell the system to treat the file as a python script.)
Thank you. Now I understand, but how correctly to write, so that the system understands that this file should be opened in Python 3?

User avatar
rpdom
Posts: 14995
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 2:55 pm

CAP33 wrote:
Sat Aug 05, 2017 2:10 pm
how correctly to write, so that the system understands that this file should be opened in Python 3?

Code: Select all

#!/usr/bin/python3

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 3:06 pm

rpdom wrote:
Sat Aug 05, 2017 2:55 pm

Code: Select all

#!/usr/bin/python3
I've modified the code a bit, the error does not appear, but the file also does not start. What is the error?

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=Cam
Type=Application
#!/usr/bin/python3
Exec=python3 /home/pi/Desktop/Python/Pi_Camera/Cam.py

User avatar
B.Goode
Posts: 8258
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 3:15 pm

We were discussing whether the Cam.py was executable and whether the python interpreter would run it. It is the first line of Cam.py that needs to be modified.

You seem to have changed the Desktop information file instead?

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 3:32 pm

B.Goode wrote:
Sat Aug 05, 2017 3:15 pm
We were discussing whether the Cam.py was executable and whether the python interpreter would run it. It is the first line of Cam.py that needs to be modified.

You seem to have changed the Desktop information file instead?
It must be so? If so, it does not work as I want. When I open the file, I fly to the desktop.

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=Cam
Type=Application
#!/usr/bin/python3 /home/pi/Desktop/Python/Pi_Camera/Cam.py

User avatar
B.Goode
Posts: 8258
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 3:51 pm

it does not work as I want. When I open the file, I fly to the desktop.
In the documentation for the Desktop file you were referred to it says:
Finally, any line beginning with an octothorpe (“#”) is considered a comment.
So your Desktop file has no details of what application to run since you have made it a comment.

I think your Desktop file should end, as you originally posted, with

Code: Select all

Exec=/home/pi/Desktop/Python/Pi_Camera/Cam.py

The file /home/pi/Desktop/Python/Pi_Camera/Cam.py should be both marked as executable, and its first line should be

Code: Select all

#!/usr/bin/python3

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 4:33 pm

B.Goode wrote:
Sat Aug 05, 2017 3:51 pm
I think your Desktop file should end, as you originally posted, with

Code: Select all

Exec=/home/pi/Desktop/Python/Pi_Camera/Cam.py
The file /home/pi/Desktop/Python/Pi_Camera/Cam.py should be both marked as executable, and its first line should be

Code: Select all

#!/usr/bin/python3
Now I understand you. In the first line of the file Cam.py I wrote

Code: Select all

#!/usr/bin/python3
And changed the contents of the file Cam.desktop

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=Cam
Type=Application
Exec=python3 /home/pi/Desktop/Python/Pi_Camera/Cam.py
But still does not work!

User avatar
B.Goode
Posts: 8258
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 4:37 pm

You are welcome to disagree with me about the format of the Desktop file, but in that case I have nothing else to suggest and I will not be making any further contribution to this discussion.

User avatar
davidcoton
Posts: 4025
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 4:45 pm

CAP33 wrote:
Sat Aug 05, 2017 4:33 pm
B.Goode wrote:
Sat Aug 05, 2017 3:51 pm
I think your Desktop file should end, as you originally posted, with

Code: Select all

Exec=/home/pi/Desktop/Python/Pi_Camera/Cam.py
... changed the contents of the file Cam.desktop

Code: Select all

Exec=python3 /home/pi/Desktop/Python/Pi_Camera/Cam.py
Compare the two versions of the "Exec=" line carefully. It looks like an editing mistake, but if so please try and take greater care.
If you are deliberately trying something different, I agree with B.Goode's last reaction.
Signature retired

CAP33
Posts: 96
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

Re: How to run the Python script with a single click?

Sat Aug 05, 2017 4:47 pm

B.Goode wrote:
Sat Aug 05, 2017 4:37 pm
You are welcome to disagree with me about the format of the Desktop file, but in that case I have nothing else to suggest and I will not be making any further contribution to this discussion.
Thanks for the info. I'm sorry that I did not understand for a long time what I should do.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: How to run the Python script with a single click?

Sun Aug 06, 2017 10:56 am

i hope you got it running now, as i was thinking actually both way should work,
and when you include the start from terminal, all 4 ways, but i found it not that easy:

when i execute in terminal:
./hallo_world.py
i get a ERROR permission denied
via desktop file with
Exec=/home/pi/Desktop/hallo_world.py
ERROR Invalid desktop entry file: '/home/pi/Desktop/hallo_world.desktop'

i can run it in IDLE3, ...
and from terminal
python3 /home/pi/Desktop/hallo_world.py
and from
/home/pi/Desktop/hallo_world.desktop

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=hallo_world
Type=Application
Terminal=false
Icon=web-browser.png
Exec=lxterminal -e "/usr/bin/python3 /home/pi/Desktop/hallo_world.py"
_____________________________
But when i make the file executable
chmod +x hallo_world.py
( or via desktop / file properties / permissions / execute "everyone" )
both (CLI or desktop file ) works and also
double click on the hallo_world.py file and select execute in terminal
also good now
/home/pi/Desktop/hallo_world.desktop

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Name=hallo_world
Type=Application
Terminal=true
Icon=web-browser.png
Exec=/home/pi/Desktop/hallo_world.py
all 4 way tested with:
/home/pi/Desktop/hallo_world.py

Code: Select all

#!/usr/bin/python3
from time import sleep
print("Hallo World")
sleep(5)
pls also note the different settings in .desktop
Terminal=true / false
i needed for that example
____________________________
so it makes a great difference
the python file ( identified by its first line ) #!/usr/bin/python3
needs executable privileges set.
one of that 2 MODS alone does not change anything!
____________________________
anyhow need minimum a DOUBLE CLICK ( from desktop)
via main menu would need minimum 2 clicks [main][other][halloworld] after copy to
/home/pi/.local/share/applications

[other] because you not specify a
Categories=xxx;
in the desktop file

a single click is only possible when you included it in the main Application Launch Bar ( after you find it under + [other] )
Image
Last edited by KLL on Tue Aug 08, 2017 9:16 am, edited 2 times in total.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: How to run the Python script with a single click?

Tue Aug 08, 2017 1:33 am

i also try to make my own MENU entry,
but NOT very lucky with the desktop tool
[main][Preferences][Main Menu Editor]
so i used a CLI tool ( yes already installed )
but that gives me a hard time about the FILE NAMEs ( i was thinking linux not care file names and extensions )
again the python thing we want to start from menu:
nano ~/Desktop/hallo_world.py

Code: Select all

#!/usr/bin/python3
from time import sleep
print("Hallo World")
sleep(5)
nano ~/.local/share/applications/my-hallo_world.desktop

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Comment=python from menu
Name=hallo_world
Type=Application
Terminal=true
Icon=web-browser.png
Categories=KLL_tools;
Exec=/home/pi/Desktop/hallo_world.py
nano ~/.local/share/desktop-directories/my-KLL_tools.directory

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Comment=my tools
Type=Directory
Name=KLL_tools
Icon=gksu
pls execute from terminal:
xdg-desktop-menu install ~/.local/share/desktop-directories/my-KLL_tools.directory ~/.local/share/applications/my-hallo_world.desktop
possibly need only a ( instead of a reboot )
lxpanelctl restart
looks like
Image
obviously it does not like "_" in names? but works.

Return to “Beginners”