JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

How to auto-boot a python3 program in Thonny

Tue Aug 27, 2019 7:13 pm

Im having great success with my program, which must ultimately be left at a customers site and boot and run by itself. The reason that I want to have it boot to Thonny...and then automatically run my program, is so that...if the customer ever has a problem, they can use VNC or a mouse, keyboard and monitor....and instantly see just what I normally see, so I can help them remotely.

There must be a simple way to have the Pi boot to Thonny and run my program. To test this, I want my VNC viewer to discover the Pi on powerup and see the Thonny program running, and show the shell window....just like I normally would see.

Please help, someone!!!!!!!

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

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

Tue Aug 27, 2019 7:23 pm

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

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

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


e. g. To run a python script add...
@sudo /usr/bin/python /home/pi/filename.py

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

To Make the file executable with sudo chmod +x filename.py

It will run after the gui starts.

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 7:42 pm

Thank you!!!!

I tried the first step, got some lines of code, made the last line @sudo...... then Ctrl-x and Y. All good. What is the significance on the last chmod line? where does it go. I typed it at the command line and it took it without error. Upon reboot, all seems the same, no harm done, but no result.

Hmmmmmm.....

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 7:52 pm

BTW.....from within Thonny, if I go to Tools and to Thonny Program folder, this is what I get:

/usr/lib/python3/dist-packages/thonny

I have no subfolders under usr/bin....

Thanks so much again....

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

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

Tue Aug 27, 2019 7:55 pm

What line did you add?

hippy
Posts: 6098
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

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

Tue Aug 27, 2019 7:59 pm

gordon77 wrote:
Tue Aug 27, 2019 7:23 pm
@sudo /usr/bin/python /home/pi/filename.py
A good habit to get into is using an explicit "python2" or "python3". It's a habit you'll be grateful for having got into at some point in the future, eg -

@sudo /usr/bin/python2 /home/pi/filename.py
@sudo /usr/bin/python3 /home/pi/filename.py
gordon77 wrote:
Tue Aug 27, 2019 7:23 pm
To Make the file executable with sudo chmod +x filename.py
It strictly isn't necessary to make a Python .py file executable when it is specified as a Python interpreter argument as here.

It's not a bad idea to do so though, providing there's a suitable shebang as the first line of the Python code, eg -

#!/usr/bin/python2
#!/usr/bin/python3

One thing to be aware of - When run as an argument to a Python interpreter, it doesn't matter if the .py file has been dragged and dropped or copied from a Windows box with Windows CR-LF line endings. When run as an executable it does, will cause ^M errors if it has Windows line endings.

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:11 pm

Still no luck, so here is what I have in /etc/xdg/lxsession/LXDE-pi/autostart

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@sudo /usr/bin/python3 /home/pi/SerialTest.py

And yes, my SerialTest27.py program is indeed in /home/pi/ directory


Great help, appreciated.....

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:12 pm

Note, I have a space in my line after Sudo and also after python3

Regards.... Jim

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

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

Tue Aug 27, 2019 8:16 pm

JimmyMac wrote:
Tue Aug 27, 2019 8:11 pm
Still no luck, so here is what I have in /etc/xdg/lxsession/LXDE-pi/autostart

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@sudo /usr/bin/python3 /home/pi/SerialTest.py

And yes, my SerialTest27.py program is indeed in /home/pi/ directory


Great help, appreciated.....
SerialTest27.py or SerialTest.py?

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:19 pm

Sorry....this is it, with proper filename..... duhhh

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

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@sudo /usr/bin/python3 /home/pi/SerialTest27.py

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

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

Tue Aug 27, 2019 8:26 pm

Why do you think your python3 program needs to run with sudo? It, most likely, doesn't need that.
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.

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

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

Tue Aug 27, 2019 8:31 pm

You do boot into the GUI?

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:43 pm

No it does not, it boots just like always. I hate to be difficult or helpless.

Jim

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:51 pm

Should this just invoke Python3 from the command line?

sudo /usr/bin/python3

It just gives me a little info on Python3

Jim

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 8:57 pm

So....just from the command line, Ive typed the sudo commands....

[email protected]:~ $ sudo /usr/bin/python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[email protected]:~ $ sudo /usr/bin/python3 /home/pi/SerialTest27.py
Traceback (most recent call last):
File "/home/pi/SerialTest27.py", line 3, in <module>
from pymodbus.client.sync import ModbusTcpClient
ModuleNotFoundError: No module named 'pymodbus.client'
[email protected]:~ $


It invokes python3 I guess, but it certainly is not Thonny. Due to this, it seems that program modules are not found....where as, from within Thonny....they are located and found just fine.

Thanks man...

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Tue Aug 27, 2019 10:20 pm

OK...so after just working at the command prompt, I can get this to launch my program successfully, though it is in the terminal window (without the Thonny gui), and it does not yet launch on reboot. Atleast I know this works from the terminal prompt:

/usr/bin/python3 /home/pi/SerialTest28.py

Somehow, the auto reboot stuff with lxsession and/or LXDE is simply not recognized.

klricks
Posts: 6609
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

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

Tue Aug 27, 2019 11:35 pm

JimmyMac wrote:
Tue Aug 27, 2019 10:20 pm
OK...so after just working at the command prompt, I can get this to launch my program successfully, though it is in the terminal window (without the Thonny gui), and it does not yet launch on reboot. Atleast I know this works from the terminal prompt:

/usr/bin/python3 /home/pi/SerialTest28.py

Somehow, the auto reboot stuff with lxsession and/or LXDE is simply not recognized.
If your program requires terminal or keyboard input or sends text to the console screen then start your program like this:

Code: Select all

@lxterminal -e python3 /home/pi/SerialTest28.py
You can do a simple test to be sure your autostart is working. Start filemanager or some other app at boot:

Code: Select all

@pcmanfm
Note for testing logout and back in is faster than rebooting.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

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

Wed Aug 28, 2019 12:14 am

Why autoload a program in Thonny?
It'll just display the program without running it.
Why not Python 3 from terminal? It could actually run the program.

JimmyMac
Posts: 11
Joined: Tue Aug 27, 2019 7:03 pm

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

Wed Aug 28, 2019 12:33 am

Dude, your the best.....you saved my tail. This crazy shit that takes much patience.

I tried rc.local......no freakin luck.

I tried systemd approach.....no freakin luck. But I did learn about bash files. Once I got a bash file created and tested.....I went back to your approach with the XLDE..etc, and with the bash file.....it works!!!!!

This stuff is nuts, but Im happy. Im a PLC guy, and this stuff can truly eat your lunch!!

Thanks again...... regards, Jim

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

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

Wed Oct 16, 2019 1:46 pm

I am running into a similar issue... I can run my program on boot up directly through python and it runs, but the functionality doesn't work. (I am running a basic program that upon press of a GPIO button, a .wav file plays.). If I enter Thonny and run the program, the audio plays with a button press. When I launch directly with Python via terminal, no audio plays via button press. So I am also looking to have the program open in Thonny and then run on boot.

User avatar
scruss
Posts: 2573
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

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

Wed Oct 16, 2019 1:57 pm

volcanic_clay wrote:
Wed Oct 16, 2019 1:46 pm
I am running into a similar issue... I can run my program on boot up directly through python and it runs,

So I am also looking to have the program open in Thonny and then run on boot.
Audio is usually handled by the X server that handles the display. Thonny runs under X. @-commands in the /etc/xdg/lxsession/LXDE-pi/autostart file run under X.

Does your Raspberry Pi automatically boot into X? Because if not, sound will be a whole different thing. Did you try the autostart method, and what happened? Did you specify python3 explicitly?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

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

Wed Oct 16, 2019 2:46 pm

I ran my command in that autostart file via python and terminal shows up but the audio does not play. If I change it to /thonny in the audio start file, it opens the program in Thonny, but it does not auto-run. When I run the program it works fine. Is that what you are asking? Apologies as I am pretty new to this.

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

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

Thu Oct 17, 2019 5:07 pm

scruss wrote:
Wed Oct 16, 2019 1:57 pm
volcanic_clay wrote:
Wed Oct 16, 2019 1:46 pm
I am running into a similar issue... I can run my program on boot up directly through python and it runs,

So I am also looking to have the program open in Thonny and then run on boot.
Audio is usually handled by the X server that handles the display. Thonny runs under X. @-commands in the /etc/xdg/lxsession/LXDE-pi/autostart file run under X.

Does your Raspberry Pi automatically boot into X? Because if not, sound will be a whole different thing. Did you try the autostart method, and what happened? Did you specify python3 explicitly?
My Pi boots to the desktop. I am my scripts in the autostart file in that directory. You can see my code and what happens during auto run and manual run via python3 and thonny. https://imgur.com/a/i3RSw1W

User avatar
scruss
Posts: 2573
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

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

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
?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

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

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

Return to “Beginners”