Error Running GPT Script on Raspberry Pi 5 – "No module named 'robot_hat'"

Hi everyone,

I’m trying to run the GPT script from the SunFounder documentation:
AI Interaction Using GPT-4O

I’m using a Raspberry Pi 5 (8GB), and other Python scripts are working fine.

I’ve successfully completed the “Installing Required Packages and Dependencies” section.

However, I’m stuck on step 4: Running the Example

Here’s what happens when I try to run the example:

bash

(my_venv) hayoon@hayoon:~/picar-x/my_venv $ cd ~/picar-x/gpt_examples/
(my_venv) hayoon@hayoon:~/picar-x/gpt_examples $ sudo ~/my_venv/bin/python3 gpt_car.py --keyboard
Traceback (most recent call last):
  File "/home/hayoon/picar-x/gpt_examples/gpt_car.py", line 10, in <module>
    from picarx import Picarx
  File "/home/hayoon/my_venv/lib/python3.11/site-packages/picarx-2.0.5-py3.11.egg/picarx/__init__.py", line 2, in <module>
    from .picarx import Picarx
  File "/home/hayoon/my_venv/lib/python3.11/site-packages/picarx-2.0.5-py3.11.egg/picarx/picarx.py", line 1, in <module>
    from robot_hat import Pin, ADC, PWM, Servo, fileDB
ModuleNotFoundError: No module named 'robot_hat'

It says the robot_hat module is not found.

How can I fix this?
Any help would be greatly appreciated!

Thanks in advance.

tried chat gpt to solve this problem. But failed. I don’t know anthying about programming.

Title: Can’t Load GPIO Pin Factory (robot_hat + gpiozero + virtualenv + PiCar-X + Raspberry Pi 5)


Hi everyone,

I’m trying to build a ChatGPT-powered voice robot using PiCar-X (Sunfounder) and a Raspberry Pi 5 with a virtual environment (venv). However, I’ve been struggling for days with GPIO access errors when using the picarx and robot_hat libraries, specifically around GPIO pin factory failures.

Here is a summary of my setup, the problem, and what I’ve tried so far.


:test_tube: Environment

  • Raspberry Pi 5 (64-bit OS)
  • Python 3.11
  • virtualenv located at ~/my_venv
  • Installed packages: picarx, robot_hat, gpiozero, RPi.GPIO, pigpio, openai, speechrecognition
  • Using gpt_car.py (from Sunfounder’s GPT + PiCar-X sample)

:warning: The Problem

When I try to run:

bash

복사편집

sudo PYTHONPATH=/usr/local/lib/python3.11/dist-packages:$PYTHONPATH ~/my_venv/bin/python3 gpt_car.py

I get the following error:

plaintext

복사편집

gpiozero.exc.BadPinFactory: Unable to load any default pin factory!
RuntimeError: Unable to load any default pin factory!

In the logs, I see:

plaintext

복사편집

Falling back from pigpio: failed to connect to localhost:8888
Falling back from native: unable to determine gpio base

:white_check_mark: What I Have Tried So Far

  1. Installed robot_hat system-wide using:

bash

복사편집

sudo python3 install.py
  1. Installed required libraries in the virtual environment:

bash

복사편집

pip install gpiozero RPi.GPIO pigpio
  1. Installed system-wide as well:

bash

복사편집

sudo pip3 install RPi.GPIO pigpio
  1. Confirmed that pigpiod is running:

bash

복사편집

sudo systemctl start pigpiod
sudo systemctl enable pigpiod
sudo pigpiod

Checked port 8888:

bash

복사편집

sudo lsof -i :8888
  1. Tried forcing gpiozero to use different pin factories:

bash

복사편집

sudo GPIOZERO_PIN_FACTORY=rpigpio PYTHONPATH=... ~/my_venv/bin/python3 gpt_car.py
sudo GPIOZERO_PIN_FACTORY=pigpio PYTHONPATH=... ~/my_venv/bin/python3 gpt_car.py
  1. Also tried copying robot_hat into the virtualenv site-packages directly.
  2. Verified RPi.GPIO can be imported:

bash

복사편집

sudo python3 -c "import RPi.GPIO as GPIO; print(GPIO.VERSION)"

:red_question_mark: What I Want to Know

  • Is there a clean way to get robot_hat + gpiozero working inside a virtualenv, while still using sudo (for hardware access)?
  • How do I correctly configure gpiozero to use a specific pin factory in this environment?
  • Am I missing a required configuration for Raspberry Pi 5 compatibility?

Any help or suggestions would be greatly appreciated :folded_hands:

Thanks in advance!

The issue occurs because your Python virtual environment doesn’t include system packages. Did you use the --system-site-packages flag when creating the virtual environment to include system packages? Please delete the current virtual environment and recreate it.AI Interaction Using GPT-4O — SunFounder PiCar-X Kit documentation