1

I have a pipenv environment which uses Python 3.7. Version 3.7 is unavailable by default in bullseye so I have tried upgrading to 3.8 and 3.9.5 with no success.

The virtual env fails to install RPi.gpio because of missing distutils packages, I decided to see it it works from outside the environment:

running Python and then

 import RPi.gpio

results in a module not found error.

But pip install RPi.gpio reports that the requirement is already satisfied RPi.gpio in /usr/lib/python3/dist-packages.

Any pointers would be appreciated on the distutils/pipenv problem or the RPi.gpio issue.

2
  • 1
    OK, I solved the issue importing from the prompt with import RPi.GPIO. but still stuck on installing it in the pipenv environment Commented Nov 16, 2021 at 11:03
  • Decided Bullseye isn't for me, the terminals don't have borders anymore and when the mouse stopped working until I rebooted I gave up. I have downloaded the Buster images and all is well for the time being. Commented Nov 16, 2021 at 18:58

1 Answer 1

1

TLDR: Always use python -m venv --system-site-packages

I realized there's a problem with venv on python3.9 of Bullseye: it stop seeing the system's installed python modules.

For example, I have some package in /usr/lib/python3/dist-packages/ which works just fine.

But as soon as I activate a venv, this path stops being seen. Notice how after I set the venv, I suddenly can't access that module anymore:

me@pi:~ $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import urllib3 ### THIS WORKS, SEE NEXT LINE

>>> urllib3.__file__
'/usr/lib/python3/dist-packages/urllib3/__init__.py'

me@pi:~ $ source ~/venv/bin/activate
(venv) me@pi:~ $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib3   ### THIS FAILS IMMEDIATELY
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib3'

This is because the sys.path gets bogged by the venv:

me@pi:~ $ python -m site
sys.path = [
    '/home/me',
    '/usr/lib/python39.zip',
    '/usr/lib/python3.9',
    '/usr/lib/python3.9/lib-dynload',
    '/usr/local/lib/python3.9/dist-packages',
    '/usr/lib/python3/dist-packages',  ### HERE IS WHERE URLLIB3 IS
    '/usr/lib/python3.9/dist-packages',
]
USER_BASE: '/home/me/.local' (exists)
USER_SITE: '/home/me/.local/lib/python3.9/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

me@pi:~$ source ~/venv/bin/activate
sys.path = [
    '/home/me',
    '/usr/lib/python39.zip',
    '/usr/lib/python3.9',
    '/usr/lib/python3.9/lib-dynload',
    '/home/me/venv/lib/python3.9/site-packages',
    ### THE LOCATION OF THE SYSTEM'S PACKAGES IS GONE
]
USER_BASE: '/home/me/.local' (exists)
USER_SITE: '/home/me/.local/lib/python3.9/site-packages' (doesn't exist)
ENABLE_USER_SITE: False

The solution is to create the venv with the --system-site-packages setting:

me@pi:~$ python -m venv --system-site-packages ~/venv
me@pi:~$ source ~/venv/bin/activate
(venv) me@pi:~$ python -m site
sys.path = [
    '/home/me',
    '/usr/lib/python39.zip',
    '/usr/lib/python3.9',
    '/usr/lib/python3.9/lib-dynload',
    '/home/me/venv/lib/python3.9/site-packages',
    '/usr/local/lib/python3.9/dist-packages',
    '/usr/lib/python3/dist-packages',
    '/usr/lib/python3.9/dist-packages',
]
USER_BASE: '/home/me/.local' (exists)
USER_SITE: '/home/me/.local/lib/python3.9/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.