Home > database >  How to run a python script with imports when usb plugged into Linux machine?
How to run a python script with imports when usb plugged into Linux machine?

Time:09-03

I have successfully gotten a shell script to run whenever a particular usb is plugged into my RPi using the following in /etc/udev/rules.d/test.rules

ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="214f", RUN ="/home/pi/script.sh"

In /home/pi/script/sh is

#!/bin/bash

echo "Hey!" >> /home/pi/hey.txt
/home/pi/python_script.py

This does successfully create a file in /home/pi called hey.txt with "Hey!" in it. However, it seems that the execution of this shell script fails trying to run /home/pi/python_script.py. I have made it executable with chmod x /home/pi/python_script.py and am even able to run the shell script from terminal and it will work perfectly fine. The first line of my python script is #!/usr/bin/python3 which is the location of my python3 executable when I do which python3. I have also tried to have the line that runs the python3 script be different things like python3 /home/pi/python_script.py and /usr/bin/python3 /home/pi/python_script.py and /usr/bin/env python3 /home/pi/python_script.py to no avail.

The error I get when I do tail -f /var/log/syslog and plug in the USB is

Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.

Also, the python3 script has code to create a file in the directory as well as write to the log so that I may verify it is running. Here is the beginning of the python3 script.

#!/usr/bin/python3
import serial
import subprocess
from time import sleep, time
import logging
from systemd.journal import JournaldLogHandler
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(module)s - %(funcName)s: %(message)s")
logging.info("Starting script!")
log = open("/home/pi/log.txt", "w")

In summary, I am able to run the script from terminal successfully and able to run the python3 script as both an executable and using python3. I am also able to see that the script does execute the first task it has when the usb is plugged in, but fails seemingly at the python3 portion.

Is there something wrong with the way I am trying to run python? I have written systemd services that successfully run python scripts, so I am confused.

EDIT: Thanks to @KlausD I see that the issue can be elaborated to that when I run it from terminal, I get no errors, but when it runs automatically from udev, I get an import error for certain modules like logging and serial, but not time.

Here is the error:

ModuleNotFoundError: No module named 'serial`

CodePudding user response:

It looks like the issue was that the python3 interpreter didn't know where to look for modules, so the answer here fixed my issue.

https://askubuntu.com/questions/1406907/python-script-called-by-udev-rule-unable-to-import-modules-modulenotfound

I put the following code at the beginning of my script and swapped out the below path for my python3 version's path and it runs!

import os
import sys
sys.path.append(os.path.abspath("/usr/local/lib/python3.9/dist-packages"))

  • Related