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?

  • 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.
    – Dirk
    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.
    – deMangler
    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?
    – Dougie
    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.
    – deMangler
    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
    – deMangler
    Dec 6, 2019 at 8:19

1 Answer 1


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.

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

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.