MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Problems with Python3 and RPi.GPIO under root

    Scheduled Pinned Locked Moved Unsolved Troubleshooting
    2 Posts 2 Posters 301 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      MyMirror
      last edited by MyMirror

      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/Linux

      Version 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.target
      

      And the start of my pirx.py looks like this:

      #!/usr/bin/python3
      # coding: utf8 #
      import RPi.GPIO as GPIO
      

      As 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

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @MyMirror
        last edited by sdetweil

        @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

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Enjoying MagicMirror? Please consider a donation!
        MagicMirror created by Michael Teeuw.
        Forum managed by Sam, technical setup by Karsten.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy