2

I have a project I am developing with python3. I would like to add some status LED functions to it.

If I am not root everything is fine, my activated virtualenv is all good, but I cannot use GPIO (for example gpiozero).

If I sudo run my code my virtualenv does not work properly.

Even if I run

sudo /home/me/.virtualenvs/project/bin/python3 mycode.py

Some libraries normally accessible from my virtualenv are not accessible (will not import)

How do I access both my virtualenv and the GPIO libraries within my python code?

asked Dec 5, 2019 at 15:53
5
  • You should not need to use sudo to wiggle pins. Are modules you need (e.g. gpiozero, RPi.GPIO, etc) available in the virtual environment? If not you may need to install them with pip3 with the virtual environment active and without using sudo. Commented Dec 5, 2019 at 16:08
  • I have never been able to use GPIO without sudo (as far as I am aware, memory security issues are the reason). I am very interested in how that is done. Commented Dec 5, 2019 at 16:28
  • What OS are you running? "sudo-less" GPIO has been there since Raspbian Jessie. Can you post the code in your python script? Commented Dec 5, 2019 at 19:25
  • @Dougie I am running latest Raspbian on a rPi4. Basically if I run a python3 shell or script I cannot import RPi.GPIO unless I run it as root. Commented Dec 6, 2019 at 5:51
  • @Dirk and Dougie... - You are correct. Thank you. I needed to add my user to gpio group to be able to run my code without sudo Commented Dec 6, 2019 at 8:19

1 Answer 1

4

Either install the libraries you need into the virtualenv using e.g. pip install gpiozero or when you create your virtualenv, enable site-packages using virtualenv --system-site-packages.

Be aware that if you install gpiozero into a virtualenv, you also need to install a pin library, probably rpi.gpio.

answered Dec 5, 2019 at 16:11
1
  • This answer got me working with my code. Later I found I could also solve the problem by adding myself to the gpio group. Commented Dec 6, 2019 at 8:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.