# BadPinFactory error when using gpiozero Python library with RaspberryPi 4

**URL:** <https://forums.balena.io/t/badpinfactory-error-when-using-gpiozero-python-library-with-raspberrypi-4/367262>\
**Category:** Project help\
**Tags:** raspberrypi4\
**Created:** [March 28, 2023, 1:54pm UTC](https://forums.balena.io/t/badpinfactory-error-when-using-gpiozero-python-library-with-raspberrypi-4/367262 "2023-03-28T13:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joel](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/joel/32/8869_2.png) [@joel](https://forums.balena.io/u/joel)\
**Post date:** [March 28, 2023, 1:54pm UTC](https://forums.balena.io/t/badpinfactory-error-when-using-gpiozero-python-library-with-raspberrypi-4/367262/1 "2023-03-28T13:54:00Z")

</div>

I’m trying to run a simple script using the RaspberryPi’s GPIO input.

**app.py**

```python
from gpiozero import Button

def main():
    button = Button(17)
    button.when_pressed = on_button_press
    button.when_released = on_button_release

def on_button_press():
    print("button 17 pressed!")

def on_button_release():
    print("button 17 released...")

if __name__ == " __main__":
    main()

```

**Dockerfile.template**

```docker
FROM balenalib/raspberrypi4-64-debian-python:3.11-bullseye-build
WORKDIR /app
RUN pip install RPi.GPIO gpiozero
COPY . .
CMD ["python3", "app.py"]

```

**docker-compose.yml**

```yaml
version: "2"
services:
  app:
    build: .
    environment:
      - GPIOZERO_PIN_FACTORY=RPiGPIO
    privileged: true
    restart: always

```

I’d really like to be able to use the [gpiozero](https://gpiozero.readthedocs.io/en/stable/) library as I find it much easier to use than RPi.GPIO. However, it seems as though there’s some sort of issue with accessing GPIO factories as when running, I initially got this error:

> [app] /usr/lib/python3/dist-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from rpigpio: No module named ‘RPi’  
> [app] warnings.warn(  
> [app] /usr/lib/python3/dist-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from lgpio: No module named ‘lgpio’  
> [app] warnings.warn(  
> [app] /usr/lib/python3/dist-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from rpio: No module named ‘RPIO’  
> [app] warnings.warn(  
> [app] /usr/lib/python3/dist-packages/gpiozero/devices.py:288: PinFactoryFallback: Falling back from pigpio: No module named ‘pigpio’  
> [app] warnings.warn(  
> [app] /usr/lib/python3/dist-packages/gpiozero/devices.py:285: NativePinFactoryFallback: Falling back to the experimental pin factory NativeFactory because no other pin factory could be loaded. For best results, install RPi.GPIO or pigpio. See [23. API - Pins — GPIO Zero 1.6.2 Documentation](https://gpiozero.readthedocs.io/en/stable/api_pins.html) for more information.  
> [app] warnings.warn(NativePinFactoryFallback(native\_fallback\_message))

This error happens even with RPi.GPIO installed.

When manually setting gpiozero to use RPi.GPIO as its factory using the `GPIOZERO_PIN_FACTORY=RPi.GPIO` env variable, I get this error;

> gpiozero.exc.BadPinFactory: Unable to find pin factory “RPi.GPIO”

As far as I can tell, this is either a Docker or BalenaOS issue as I’ve not had this problem when running gpiozero natively on Raspberry Pi OS.

Changing the code to use the RPi.GPIO library directly works fine. Given the simple syntax of gpiozero though I’d love the option of using it.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![alanb128](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/alanb128/32/2839_2.png) [@alanb128](https://forums.balena.io/u/alanb128)\
**Post date:** [April 19, 2023, 1:42am UTC](https://forums.balena.io/t/badpinfactory-error-when-using-gpiozero-python-library-with-raspberrypi-4/367262/2 "2023-04-19T01:42:03Z")

</div>

You should be able to get gpiozero running in a container although I haven’t tried it. Here is an example that might help: [balena-weather/raingauge at master · hferentschik/balena-weather · GitHub](https://github.com/hferentschik/balena-weather/tree/master/raingauge)

---

<div class="post-metadata">

**Author:** ![joel](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/joel/32/8869_2.png) [@joel](https://forums.balena.io/u/joel)\
**Post date:** [April 19, 2023, 9:29am UTC](https://forums.balena.io/t/badpinfactory-error-when-using-gpiozero-python-library-with-raspberrypi-4/367262/3 "2023-04-19T09:29:31Z")

</div>

Hi alanb128,

Thanks for the response.

After reviewing the code you linked there didn’t seem to be any different from what the `raingauge.py` script was doing compared to the code I provided from a library / package perspective.

One thing I realised I’d left off my `app.py` code was a way to keep the code from dropping off the end of the main() function.

Updated code:

**app.py**

```auto
from gpiozero import Button
from signal import pause

def main():
    button = Button(17)
    button.when_pressed = on_button_press
    button.when_released = on_button_release
    print(f'{button=}')
    pause()

def on_button_press():
    print("button 17 pressed!")

def on_button_release():
    print("button 17 released...")

if __name__ == " __main__":
    main()

```

**docker-compose.yml**

```auto
version: "2"
services:
  app:
    build: .
    privileged: true
    restart: always

```

What’s very odd is that the PinFactory error is now gone! No idea if this is simply related to adding the `pause()` function call or something else in the backend.

Long story short… **issue is solved!**

Thanks for confirming that `gpiozero` should work to motivate me to try things again.
