I am having a major issue with the GPIO in virtual env

When I run this code outside my virtual environment, it works. However, when I try to run it inside my virtual env I receive an error. I googled, and ChatGPT for hours. No success. Any thoughts?

Code ran:

from gpiozero import LED
from time import sleep
from signal import signal, SIGTERM, SIGHUP, pause

def safe_exit(signum, frame):
    exit(1)

red = LED(26)

def flashingLights():
    while True:
        red.on()

Error received

/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from lgpio: module 'lgpio' has no attribute 'SET_BIAS_DISABLE'
  warnings.warn(
Traceback (most recent call last):
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/pins/pi.py", line 411, in pin
    pin = self.pins[info]
          ~~~~~~~~~^^^^^^
KeyError: PinInfo(number=37, name='GPIO26', names=frozenset({'BOARD37', 'WPI25', 'GPIO26', '26', 26, 'J8:37', 'BCM26'}), pull='', row=19, col=1, interfaces=frozenset({'', 'sdio', 'gpio', 'dpi', 'spi', 'jtag'}))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/cisco/Desktop/ledlight.py", line 8, in <module>
    red = LED(26)
          ^^^^^^^
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/devices.py", line 108, in __call__
    self = super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/output_devices.py", line 192, in __init__
    super().__init__(pin, active_high=active_high,
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/output_devices.py", line 74, in __init__
    super().__init__(pin, pin_factory=pin_factory)
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/mixins.py", line 75, in __init__
    super().__init__(*args, **kwargs)
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/devices.py", line 553, in __init__
    pin = self.pin_factory.pin(pin)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/pins/pi.py", line 413, in pin
    pin = self.pin_class(self, info)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cisco/Desktop/myenv/lib/python3.11/site-packages/gpiozero/pins/rpigpio.py", line 101, in __init__
    GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[self._pull])
RuntimeError: Cannot determine SOC peripheral base address
(myenv) cisco@raspberrypi:~/Desktop $ 

my requirement file:

arandr             0.1.11
asgiref            3.7.2
av                 11.0.0
Babel              2.14.0
beautifulsoup4     4.12.3
blinker            1.7.0
certifi            2024.2.2
cffi               1.16.0
chardet            5.2.0
charset-normalizer 3.3.2
click              8.1.7
colorama           0.4.6
colorzero          2.0
cryptography       42.0.5
cupshelpers        1.0
dbus-python        1.3.2
distro             1.9.0
Flask              3.0.2
gpiozero           2.0.1
html5lib           1.1
idna               3.6
importlib_metadata 7.0.2
itsdangerous       2.1.2
Jinja2             3.1.3
lgpio              0.0.0.2
libevdev           0.11
lxml               5.1.0
MarkupSafe         2.1.5
more-itertools     10.2.0
numpy              1.26.4
oauthlib           3.2.2
olefile            0.47
pexpect            4.9.0
pgzero             1.2.1
picamera2          0.3.17
pidng              4.0.9
piexif             1.1.3
pigpio             1.78
pillow             10.2.0
pip                24.0
ptyprocess         0.7.0
pycairo            1.20.1
pycparser          2.21
pycups             2.0.1
pygame             2.5.2
PyGObject          3.42.2
pyinotify          0.9.6
PyJWT              2.8.0
PyOpenGL           3.1.7
pyOpenSSL          24.0.0
PyQt5              5.15.9
PyQt5_sip          12.13.0
pyserial           3.5
pysmbc             1.0.23
python-apt         2.6.0
python-dotenv      1.0.1
python-prctl       1.8.1
pytz               2024.1
pyudev             0.24.1
PyYAML             6.0.1
reportlab          4.1.0
requests           2.31.0
requests-oauthlib  1.3.1
responses          0.25.0
RPi.GPIO           0.7.1
rpi-lcd            0.0.3
RTIMULib           7.2.1
sense-hat          2.6.0
setuptools         66.1.1
simplejpeg         1.7.2
simplejson         3.19.2
six                1.16.0
smbus              1.1.post2
smbus2             0.4.3
soupsieve          2.5
spidev             3.6
ssh-import-id      5.11
toml               0.10.2
twython            3.9.1
typing_extensions  4.10.0
urllib3            2.2.1
v4l2-python3       0.3.4
webencodings       0.5.1
Werkzeug           3.0.1
wheel              0.42.0
zipp               3.17.0

Upvotes: 1

Views: 697

Answers (2)

Wangdong Xu
Wangdong Xu

Reputation: 11

I ran into the same issue. Using /usr/bin/python (the system python) will not throw this error, but you probably want to maintain your packages in a virtual env.

My workaround is to run with /usr/bin/python anyway but I also import the packages from my virtual env.

import sys
sys.path.append('/path/to/venv/lib/python3.x/site-packages')

# Now, you can import the package from the venv
import some_package_from_venv

Upvotes: 1

SVBazuev
SVBazuev

Reputation: 126

The code you provided did not allow me to repeat the error you received.

Try to do this:

red = LED('GPIO26')

But I'm confused by your requirement file, it's too colorful !?

I would do so:

  1. deactivated venv
  2. deleted the venv folder
  3. reinstalled the dependencies required for this project
  4. created a new requirements file

The hygiene of the virtual environment is very important...

Good luck!

Upvotes: 1

Related Questions