I have a udev rule which triggers a python script whenever a block device (USB drive) is attached/detached from the system. The udev rule seems to be firing correctly, but the python script it calls cant start because it's missing an import. The import error I get it ModuleNotFound error. The udev script calls a bash script which calls:
python3 /root/projects/script.py --arg "$1" --arg2 >> /tmp/python3.log 2>&1 &
In python3.log, I see the error:
ModuleNotFoundError: No module named 'requests'
I have installed this module as root via pip3 install requests
, and indeed the script works fine when manually called from the root shell, but somehow being called by udev doesn't work.
If I run from root terminal pip3 install requests
I get Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (2.27.1)
I assume this is a pathing issue? How do I go about figuring out why python can't find this module?
The problem is the way python determines it's "path". I don't know the best practice solution for this, but here's what I did. From a python console that does work (
sudo python3
) I doimport requests
thenprint(requests)
to get the full path. This gives me<module 'requests' from '/usr/local/lib/python3.9/dist-packages/requests/__init__.py'>
So in my script.py I add this to the top
And the issue is solved