Using GPIO pins from Python

This forum is for topics dealing with problems with software specifically in the ARMv7h repo.

Using GPIO pins from Python

Postby unformatted » Tue Aug 17, 2021 3:54 pm

I'm using GPIO from Python for years on various Pi's using the python-raspberry-gpio package from AUR as described in the wiki (https://archlinuxarm.org/wiki/Raspberry_Pi). I noticed this package has been removed from AUR so the wiki needs updating.

The official Raspberry Pi documentation says to use the gpio-zero library (https://www.raspberrypi.org/documentati ... -in-python). I do use python-gpiozero from AUR on some of my Pi's but that's been last updated end of 2019.

So my question is: what is the preferred way to control the GPIO pins from Python while on a RPi using armv7?
unformatted
 
Posts: 109
Joined: Tue Mar 09, 2021 5:23 pm

Re: Using GPIO pins from Python

Postby robg » Tue Aug 17, 2021 9:17 pm

I don't use the GPIO interface myself, but recall reading that it should now be installed via pip.
If the above advice is correct, I suggest you contribute it to the wiki.
robg
 
Posts: 128
Joined: Tue Jan 05, 2021 8:22 am

Re: Using GPIO pins from Python

Postby unformatted » Wed Aug 18, 2021 9:41 am

Well, I tried installing RPi.GPIO using pip, which I had to install first, but that's not working. I never worked with pip so I followed the python docs (https://packaging.python.org/tutorials/ ... -packages/). First I upgraded my RPi4 (armv7), rebooted and then:

Code: Select all
$ python3 --version
   Python 3.9.6
$ python3 -m pip --version
   /usr/bin/python3: No module named pip
# pacman -S python-pip
# reboot
$ python3 -m pip --version
   pip 20.3.4 from /usr/lib/python3.9/site-packages/pip (python 3.9)
$ python -m pip install RPi.GPIO
   Defaulting to user installation because normal site-packages is not writeable
   Collecting RPi.GPIO
     Using cached RPi.GPIO-0.7.0.tar.gz (30 kB)
   Using legacy 'setup.py install' for RPi.GPIO, since package 'wheel' is not installed.
   Installing collected packages: RPi.GPIO
      Running setup.py install for RPi.GPIO ... error
      ERROR: Command errored out with exit status 1:


After that follows a huge list or red lines which I could pastebin but I'm afraid I'm doing something wrong here..
unformatted
 
Posts: 109
Joined: Tue Mar 09, 2021 5:23 pm

Re: Using GPIO pins from Python

Postby robg » Wed Aug 18, 2021 9:53 am

I believe you need to install it as root (cf. the linked installation instruction which use `sudo`.)
robg
 
Posts: 128
Joined: Tue Jan 05, 2021 8:22 am

Re: Using GPIO pins from Python

Postby unformatted » Wed Aug 18, 2021 10:26 am

I installed python-pip using sudo.
I also tried
Code: Select all
sudo python -m pip install RPi.GPIO

and even
Code: Select all
sudo pip3 install rpi.gpio

which all give the same error output.

I run arch as a regular user "user" but I'm member of the wheel group, I'm also added to the sudoers file as "user" ( user ALL=(ALL) ALL) ). I've been installing packages for years this way (using sudo), so what am I missing here?

EDIT: I just did
Code: Select all
[user@PC19 ~]$ sudo su
[sudo] password for user:
[root@PC19 user]# python -m pip install RPi.GPIO

and still the same error output.
unformatted
 
Posts: 109
Joined: Tue Mar 09, 2021 5:23 pm

Re: Using GPIO pins from Python

Postby unformatted » Wed Aug 18, 2021 4:10 pm

@robg wrote: "I believe you need to install it as root (cf. the linked installation instruction which use `sudo`.)"
I'm sorry but I didn't understand the meaning of "cf" at first, English is not my native language ;-)

So finally the penny dropped. I rebooted and logged in as the "root" user and tried again. But the errors remain exactly the same. I also tried other packages like gpiozero but same errors.

Here are the errors:
https://pastebin.com/8CWjaVCT

I just tried installing the same on a different Pi (3B) and got exactly the same errors.
So perhaps anyone with a (recently updated) Pi is able to install a Python package using pip?
unformatted
 
Posts: 109
Joined: Tue Mar 09, 2021 5:23 pm

Re: Using GPIO pins from Python

Postby robg » Wed Aug 18, 2021 7:22 pm

Can you please post the output of `pacman -Qs linux`? Is the "base-devel" package group installed on your system?
robg
 
Posts: 128
Joined: Tue Jan 05, 2021 8:22 am

Re: Using GPIO pins from Python

Postby bulletmark » Wed Aug 18, 2021 10:52 pm

The easiest and quickest way is to install from wheel instead of source. Just:

Code: Select all
sudo pacman -S python-pip python-wheel

Then:

Code: Select all
sudo pip install -U rpi-gpio

If you want to access the GPIO as your normal user then you need to be in the gpio group:

Code: Select all
sudo groupadd -f -r gpio
sudo usermod -aG gpio $USER

The copy this rules file to /etc/udev/rules.d/99-gpio.rules and reboot.
bulletmark
 
Posts: 86
Joined: Tue Oct 13, 2015 10:17 pm

Re: Using GPIO pins from Python

Postby unformatted » Thu Aug 19, 2021 9:19 am

@robg:

Code: Select all
[user@PC19 ~]$ pacman -Qs linux
local/alsa-lib 1.2.5.1-3
    An alternative implementation of Linux sound support
local/archlinuxarm-keyring 20140119-1
    Arch Linux ARM PGP keyring
local/avahi 0.8+20+gd1e71b3-1
    Service Discovery for Linux using mDNS/DNS-SD -- compatible with Bonjour
local/base 2-2
    Minimal package set to define a basic Arch Linux installation
local/filesystem 2021.05.31-1
    Base Arch Linux files
local/iptables 1:1.8.7-1
    Linux kernel packet control tool (using legacy interface)
local/keyutils 1.6.3-1
    Linux Key Management Utilities
local/kmod 29-1
    Linux kernel module management tools and library
local/libaio 0.3.112-2
    The Linux-native asynchronous I/O facility (aio) library
local/libcap-ng 0.8.2-3
    A library for Linux that makes using posix capabilities easy
local/libva 2.12.0-1
    Video Acceleration (VA) API for Linux
local/libxshmfence 1.3-2
    a library that exposes a event API on top of Linux futexes
local/linux-api-headers 5.12.3-1
    Kernel headers sanitized for use in userspace
local/linux-firmware 20210716.b7c134f-1
    Firmware files for Linux
local/linux-raspberrypi4 5.10.59-1
    The Linux Kernel and modules - Raspberry Pi 4
local/lirc 1:0.10.1-8
    Linux Infrared Remote Control utilities
local/net-tools 2.10-1
    Configuration tools for Linux networking
local/pacman-mirrorlist 20210307-1 (base)
    Arch Linux ARM mirror list for use by pacman
local/python-distro 1.5.0-3
    Linux OS platform information API
local/util-linux 2.37.1-3
    Miscellaneous system utilities for Linux
local/util-linux-libs 2.37.1-3
    util-linux runtime libraries

Code: Select all
[user@PC19 ~]$ pacman -Qs base-devel
local/autoconf 2.71-1 (base-devel)
    A GNU tool for automatically configuring source code
local/automake 1.16.4-1 (base-devel)
    A GNU tool for automatically creating Makefiles
local/binutils 2.35-1 (base-devel)
    A set of programs to assemble and manipulate binary and object files
local/bison 3.7.6-1 (base-devel)
    The GNU general-purpose parser generator
local/fakeroot 1.25.3-2 (base-devel)
    Tool for simulating superuser privileges
local/file 5.40-5 (base-devel)
    File type identification utility
local/findutils 4.8.0-1 (base-devel)
    GNU utilities to locate files
local/flex 2.6.4-3 (base-devel)
    A tool for generating text-scanning programs
local/gawk 5.1.0-1 (base-devel)
    GNU version of awk
local/gcc 10.2.0-1 (base-devel)
    The GNU Compiler Collection - C and C++ frontends
local/gettext 0.21-1 (base-devel)
    GNU internationalization library
local/grep 3.6-1 (base-devel)
    A string search utility
local/groff 1.22.4-6 (base-devel)
    GNU troff text-formatting system
local/gzip 1.10-3 (base-devel)
    GNU compression utility
local/libtool 2.4.6+44+gb9b44533-14 (base-devel)
    A generic library support script
local/m4 1.4.19-1 (base-devel)
    The GNU macro processor
local/make 4.3-3 (base-devel)
    GNU make utility to maintain groups of programs
local/pacman 6.0.0-5 (base-devel)
    A library-based package manager with dependency support
local/patch 2.7.6-8 (base-devel)
    A utility to apply patch files to original sources
local/pkgconf 1.7.3-1 (base-devel)
    Package compiler and linker metadata toolkit
local/sed 4.8-1 (base-devel)
    GNU stream editor
local/sudo 1.9.7.p2-1 (base-devel)
    Give certain users the ability to run some commands as root
local/texinfo 6.8-2 (base-devel)
    GNU documentation system for on-line information and printed output
local/which 2.21-5 (base-devel)
    A utility to show the full path of commands


I don't believe there's anything wrong with the installation. I always follow the installation instructions (https://archlinuxarm.org/platforms/armv ... berry-pi-4, ARMv7) to the letter. Then I change the root password, delete the alarm user and add my own user to the users and wheel group, install sudo and add my user to the sudoers file (user ALL=(ALL) ALL) and finally lock the root account. Then I reboot and log in as my own user which now is a restricted user with elevated rights (sudo). All according arch wiki recommendations. I use this on all my Pi's for years and never had to install something using the root account.

You say you don't use the GPIO pins but you do have a RPi? Would be nice to know if you are able to install a python package using pip like described in the python docs:
Code: Select all
(sudo) python -m pip install RPi.GPIO

uninstall should remove it from your system.

@bulletmark:

I'm not a programmer and will have to read about this python-wheel method first before I use commands which I do not understand This pip thing is all new to me and my rusty brain needs some time to figure it out. First I would like to know why the above described command gives the errors.

I'm very interested in accessing the GPIO pins as regular user so after this is solved I will certainly try it. Thank for the suggestion.
unformatted
 
Posts: 109
Joined: Tue Mar 09, 2021 5:23 pm

Re: Using GPIO pins from Python

Postby robg » Fri Aug 20, 2021 1:07 pm

unformatted wrote:I don't believe there's anything wrong with the installation. I always follow the installation instructions (https://archlinuxarm.org/platforms/armv ... berry-pi-4, ARMv7) to the letter. Then I change the root password, delete the alarm user and add my own user to the users and wheel group, install sudo and add my user to the sudoers file (user ALL=(ALL) ALL) and finally lock the root account. Then I reboot and log in as my own user which now is a restricted user with elevated rights (sudo). All according arch wiki recommendations. I use this on all my Pi's for years and never had to install something using the root account.

I just wanted to confirm that you are running the upstream RPI kernel (linux-raspberrypi4) as this is - at least in principle - the one supported by Python's GPIO. I am running the 64bit RPI kernel (Linux alarm 5.10.59-1-ARCH #1 SMP PREEMPT Tue Aug 17 23:19:33 UTC 2021 aarch64 GNU/Linux) and can confirm that with "python" and "python-pip" installed, I can install "gpiozero" via pip:
Code: Select all
[root@alarm alarm]# pip3 install gpiozero
WARNING: The directory '/home/alarm/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.     
Collecting gpiozero
  Downloading gpiozero-1.6.2-py2.py3-none-any.whl (148 kB)
     || 148 kB 6.8 MB/s
Collecting colorzero
  Downloading colorzero-2.0-py2.py3-none-any.whl (26 kB)
Requirement already satisfied: setuptools in /usr/lib/python3.9/site-packages (from colorzero->gpiozero) (57.4.0)
Installing collected packages: colorzero, gpiozero
Successfully installed colorzero-2.0 gpiozero-1.6.2
[root@alarm alarm]# python3
Python 3.9.6 (default, Jul  6 2021, 17:06:46)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gpiozero import Button
>>>


I will have a more detailed look at your error log and get back to you later.
robg
 
Posts: 128
Joined: Tue Jan 05, 2021 8:22 am

Next

Return to ARMv7h

Who is online

Users browsing this forum: No registered users and 6 guests