Search code examples
pythonpython-3.xorange-pilibgpiod

'gpiod' is not a package


I'm trying to use libgpiod in python on my orangepi model 4B. I installed it with:

sudo apt install libgpiod-dev python3-libgpiod

Now I try to use it:

from gpiod.line import Direction, Value

But I get an error:

ModuleNotFoundError: No module named 'gpiod.line'; 'gpiod' is not a package

If I open python in terminal and import gpiod the autocomplete options for gpiod. are:

gpiod.Chip(                      gpiod.LINE_REQ_FLAG_OPEN_DRAIN
gpiod.ChipIter(                  gpiod.LINE_REQ_FLAG_OPEN_SOURCE
gpiod.LINE_REQ_DIR_AS_IS         gpiod.Line(
gpiod.LINE_REQ_DIR_IN            gpiod.LineBulk(
gpiod.LINE_REQ_DIR_OUT           gpiod.LineEvent(
gpiod.LINE_REQ_EV_BOTH_EDGES     gpiod.LineIter(
gpiod.LINE_REQ_EV_FALLING_EDGE   gpiod.find_line(
gpiod.LINE_REQ_EV_RISING_EDGE    gpiod.version_string(
gpiod.LINE_REQ_FLAG_ACTIVE_LOW  

If I install gpiod through pip it says module 'gpiod' has no attribute 'Chip' when I try to use gpiod.Chip.

What is wrong? Thanks in advance.


Solution

  • libgpiod-dev includes the the libgpiod C lib and its header files, while python3-libgpiod includes the Python bindings to that lib. So with the command:

    sudo apt istall libgpiod-dev python3-libgpiod
    

    You're installing the libgpiod lib, C headers and python bindings. This lib is an abstraction layer to use gpio char device in Linux. Examples for controlling gpios with these python bindings can be found here.

    Try copy-paste one of the examples and run it. Make sure package is installed correctly and you have properly configured char device interface (ls /dev here you must see gpiochipX devices). I just tested it and worked as expected.

    NOTE:

    When you use pip install gpiod you can't use gpiod.Chip, but you can use gpiod.chip instead (note that the former is capital). This is because you are using an old version of gpiod python package. You said that you're using gpiod version 1.5.4 so this is the corresponding documentation for that version. As you can see it claims to be a:

    It is a pure Python library and has no dependencies on other packages !!

    It also includes a link to the proper documentation site where you can see that the examples are using gpiod.chip instead of gpiod.Chip.

    The new version of gpiod package (2.1.3) is not a pure python package but official bindings to libgpiod. as you can see in the proper doc. This version DOES support the usage of gpiod.Chip as you can see in its examples.

    So, how to be able yo use gpio.Chip through a pip installation?

    pip install gpiod==2.1.3
    

    This guarantees you use the new version