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:-
- To put the “source …/bin/activate” command directly in the trigger script - as shown above [which does not seem to work]
- 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…