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

I just tried your recommended solution, but it doesn’t seem to work for me… and I wondered if you might be able to suggest where I’m going wrong, please?

I have a couple of BME688’s, one on a Raspberry Pi running Bookworm, where I set up the environment in early 2023; the other on a new/replacement Pi running Trixie, which I set up last week.

I have written a simple Python script that captures the atmospheric data and appends it to a .csv file [with the files being named based on the month in which they are generated]. This works flawlessly on the Bookworm system, but when I ported the script to the Trixie machine, I get the following error:-

$ sudo /opt/scripts/aSCtrigger.sh
Traceback (most recent call last):
File “/opt/scripts/atmosphereSensorCapture.py”, line 24, in
import bme680 # Imports the Pimoroni Python library for the BME680 Sensor
^^^^^^^^^^^^^
ModuleNotFoundError: No module named ‘bme680’

The opening portion of my Python script [comments excluded] is as follows:-

import os
import subprocess
import sys
import bme680
import time

And as you can see from the error transcript, this is failing to find the bme680 module.

However, what I’m trying to do is replicate my Bookworm setup, where I have a trigger script that is invoked by cron [and thus runs as root], where the invoked cron script looks like this:-

#!/usr/bin/bash
mount -a
sleep 10
source /opt/pimoroni/bin/activate
echo “PATH = {” . $PATH . “}”
/usr/bin/python3 /opt/scripts/atmosphereSensorCapture.py

Now, because I want this script to be invoked by root [using crontab] - and because the unhelpful Pimoroni installer script insists on putting the sensor’s code in my regular user’s library - I’ve tried:-

  1. To put the “source …/bin/activate” command directly in the trigger script - as shown above [which does not seem to work]
  2. To put the “source …/bin/activate” command in to the file “/root/.bashrc”, based on your recommendations here [which also does not seem to work].

The frustration is that it does work when invoking interactively [and of course after manually performing the “source” command…] but not when trying to get the virtual environment activated within the context of an invoked script.

This seems to be a fundamental flaw in the use of the virtual environment - especially given the fact that the error does not manifest on the Bookworm installation which was created prior to the virtualisation being included.

Would you please be able to suggest a configuration that would allow me to use cron to schedule an activation of python script that is able to work with the environment changes being made by the Pimoroni ../bin/activate script?

Thank you in advance…

Does this help? You can specify the path to your virtual environment in your crontab entry: Displaying Comics on Inky Impression