How to autostart Enviro+ example scripts?

Hi there,

I tried to autostart one of the example scripts come with Enviro+ Hat, “all-in-one-no-pm.py” but it didn’t.

I tried to do it by editing rc.local but since I only tried to run the python code, and couldn’t start virtual environment from there, the code didn’t start.

Is there a better way to do it?

I usually use cron. I’m far from any knid of expert on this, but its what works for me.
From a terminal I run
crontab -e
and add
@reboot python3 /home/pi/my.py

If that doesn’t work try running
sudo crontab -e

My problem is that I have to create a virtual environment first with this command:

source ~/.virtualenvs/pimoroni/bin/activate

so it’s not just basic python code execution.

Ah , OK. Pi5 running latest Bookworm?
I’ve been running Legacy OS when I can to avoid having to use a virtual environment. It’s not the best approach, but for now it’s what I have been doing. Just being honest.
So far I only have the one Pi 5, and haven’t added anything third party to it.

If you add the line source ~/.virtualenvs/pimoroni/bin/activate at the end of the .bashrc file located within the home folder of your login, it will enable the virtual enviroment at boot up.

Hope this helps.

I don’t get it, can you elaborate? I tried adding the line

source ~/.virtualenvs/pimoroni/bin/activate

to “/etc/rc.local”, right before the python code is executed but it didn’t work.

If your using the login of pi for example, then the line source ~/.virtualenvs/pimoroni/bin/activate should be added into the .bashrc file in the /home/pi users folder.

If there is a (pimoroni) before the command prompt it’s worked okay and the venv is activated.

As shown below. Hope this helps. 👍

1 Like