User avatar
Paeryn
Posts: 2699
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 2:25 am

volcanic_clay wrote:
Thu Oct 17, 2019 11:16 pm
scruss wrote:
Thu Oct 17, 2019 7:22 pm
commands in autostart need the @ in front of them. But the not running from the command line is odd.

Does it work if you add

Code: Select all

#!/usr/bin/python3
as the very first line of your TrackPlayer.py, make it executable with chmod +x TrackPlayer.py, then run it in the terminal with

Code: Select all

~/Projects/TrackPlayer.py
?
The '#!' makes it not run at all from terminal.
When I try to add chmod +x as the top line of code in Thonny it tells me 'chmod' is not defined.
If I add 'chmod +x TrackPlayer.py' is throws a ^ under the 'r' of 'Player' and says "invalid syntax".
The line

Code: Select all

chmod +x TrackPlayer.py
is meant to be typed at the command line in a terminal (and with you in the same directory as the file), not put in the file, it adds the execute permission to the file as without this permission linux will refuse to run the file directly. The shebang line (the one starting #!) tells linux what program it really needs to run (and the file will be passed on to that), without it an executable text file is assumed to be a shell script.
scruss wrote: commands in autostart need the @ in front of them.
No they don't, the @ symbol at the start of a line in autostart just tells lxsession that if the command crashes (exits with a return value of anything other than 0) then the command is to be re-started automatically.
She who travels light — forgot something.

volcanic_clay
Posts: 11
Joined: Tue Oct 01, 2019 12:38 am

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 4:53 pm

In terminal I am typing:

/usr/bin/thonny /home/pi/Projects/chmod + x TrackPlayer.py - This opens the program in Thonny but does not run it.

#!/usr/bin/thonny /home/pi/Projects/chmod + x TrackPlayer.py - Nothing happens.

#!/usr/bin/python3 /home/pi/Projects/TrackPlayer.py - Nothing happens.

#!/usr/bin/python3 /home/pi/Projects/chmod + x TrackPlayer.py - Nothing happens.

/usr/bin/thonny chmod + x /home/pi/Projects/TrackPlayer.py - This opens the program in Thonny but does not run it.

User avatar
Paeryn
Posts: 2699
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 5:21 pm

The chmod command given is entered on its own, not as an argument to either python3 or thonny.
From how you are trying to do it I assume the file you are wanting to run is in /home/pi/Projects. Enter these lines individually at the command line in a terminal. The first line makes sure you in the directory where the file is, the second line adds the execute permission and the third line runs the file (assuming that you have the shebang line as the first line in the file).

Code: Select all

cd ~/Projects
chmod +x TrackPlayer.py
./TrackPlayer.py
You only need to do the chmod command once as once it is set it will stay set.
She who travels light — forgot something.

volcanic_clay
Posts: 11
Joined: Tue Oct 01, 2019 12:38 am

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 6:40 pm

Still just opening up Thonny and not running... https://imgur.com/a/dL9HlVR

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 6:53 pm

As before with more detail...

I don't believe you need to boot Thonny, just your python3 script.

Try using /etc/xdg/lxsession/LXDE-pi/autostart

in a terminal...

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart


e. g. To run a python script add at the end of the file...

@ /usr/bin/python3 /home/pi/Projects/TrackPlayer.py

Then press ctrl & X, and then Y and enter.

To Make the file executable by typing sudo chmod +x filename.py in a terminal and Press Enter

It will run after the gui starts.
Last edited by gordon77 on Fri Oct 18, 2019 7:43 pm, edited 2 times in total.

User avatar
Paeryn
Posts: 2699
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 7:04 pm

That looks like you haven't put the shebang line at the start of the file. It needs to be the very first line and the #! has to be the first two characters. If you so much as have a blank line above it or a space before the characters then it won't be recognised, the #! literally has to be the first two characters of the file. So your file should start along the lines of (the second line here should be the first line of your program, from your screenshot I can see it is an import but might not be gpiozero). Python itself will ignore the shebang line as it sees it as a comment.

Code: Select all

#!/usr/bin/python3
import gpiozero
... rest of your program here ...
She who travels light — forgot something.

volcanic_clay
Posts: 11
Joined: Tue Oct 01, 2019 12:38 am

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 8:28 pm

@Paeryn I found one of your old posts on a similar topic and got everything sorted out! What was happening was when I was running it from Terminal via python3 the program would exit after setting the callback. I just had to add signal.pause and now it runs fine from terminal and therefore works upon boot up! Thanks all for helping me along as a total novice!

User avatar
Paeryn
Posts: 2699
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How to auto-boot a python3 program in Thonny

Fri Oct 18, 2019 9:31 pm

Glad you managed to sort it out. That's the main downside of IDLE and Thonny (and possibly other python IDEs), when running programs in them your program never really exits unless you restart the shell (either manually or by re-running your progran).

Whilst this makes it easier to debug your program when it crashes (objects are still bound etc.) it means that successful exits don't automatically clean up fully like they do outside of the IDE.
She who travels light — forgot something.

Return to “Beginners”