Read the statement by Michael Teeuw here.
Problems with Python3 and RPi.GPIO under root
-
Hi all,
may i ask for a little help?
I am really not a Linux specialist and would be glad if you could help me with this problem.Environment: Raspi 3b with Bullseye
uname -a gives the following output: Linux MM3 6.1.21-v7+ #1642 SMP Mon Apr 3 17:20:52 BST 2023 armv7l GNU/LinuxVersion of my MagicMirror: 2.25.0
If I start Python3 as normal user “pi” and call “import RPi.GPIO as GPIO”, then it works but not as root:
pi@MM3:~/MagicMirror $ pwd /home/pi/MagicMirror pi@MM3:~/MagicMirror $ whoami pi pi@MM3:~/MagicMirror $ python3 Python 3.9.7 (default, Oct 14 2023, 17:06:45) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import RPi.GPIO as GPIO >>> pi@MM3:~/MagicMirror $ su - root Passwort: root@MM3:~# pwd /root root@MM3:~# python3 Python 3.9.7 (default, Oct 14 2023, 17:06:45) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import RPi.GPIO as GPIO Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'RPi' >>> root@MM3:~Unfortunately, I cannot start such a python script for a PIR as a service. What is the user root missing that the user “pi” has?
I want the Python script here: /lib/systemd/system to start as a service when starting the raspi: pir_sensor.service This is not done with the user “pi” but as “root” or “system”, right?
Contents of pir_sensor.service:[Unit] Description=MagicMirror PIR Service After=multi-user.target [Service] Type=idle WorkingDirectory=/home/pi/MagicMirror Restart=on-failure User=root ExecStart=/usr/bin/python3.9 /home/pi/MagicMirror/pirx.py [Install] WantedBy=multi-user.targetAnd the start of my pirx.py looks like this:
#!/usr/bin/python3 # coding: utf8 # import RPi.GPIO as GPIOAs user pi it works well, but as described above, not as root or “system”.
Can anyone help me to repair Python to work under root/system as well with RPi.GPIO?Thanks
Jens -
@MyMirror use pm2 to launch your script. it will launch as the user
see pm2 --help
after the appropriate pm2 start
do pm2 save
to have that task launched at boot
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login