26

I downloaded RPi.GPIO 5.3a from here: https://pypi.python.org/pypi/RPi.GPIO

I extracted the tar file, cd into the folder and ran:

sudo python setup.py install

It ran through a bunch of stuff and doesn't seem to have failed. The last line of its work is this:

Writing /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.5.3a.egg-info

After that it was done and gave me back control.

Now I type

sudo python

then

import RPi.GPIO

and I see

ImportError: No module named GPIO

Can anyone point me to exactly what steps I need to take to get the python GPIO library installed correctly so I can import it and manipulate the GP pins? I've found a mish mash of different processes online (this was the one that came up most often) but I have thus far been unable to get any of them to work.

2
  • the correct syntax is: import RPi.GPIO as GPIO Commented Jun 19, 2014 at 21:10
  • 1
    Here in the future, RPi.GPIO, and gpiozero are now installed by default in Raspbian. See raspberrypi.org/blog/gpio-zero-update Commented Nov 8, 2017 at 1:07

6 Answers 6

37

Not sure if this is helpful, but under the latest copy of Raspbian I was able to install RPi.GPIO directly from the main repositories using apt-get as follows:

sudo apt-get update
sudo apt-get -y install python-rpi.gpio

If you're running Python 3 (idle3 on the command line) instead of Python 2 (python on the command line) you need to install the RPi.GPIO library with this command instead:

sudo apt-get -y install python3-rpi.gpio
1
  • 1
    E: Package 'python-rpi.gpio' has no installation candidat Commented May 5, 2022 at 18:59
12

Assuming you have pip, the python package index installer, which is installed on the latest versions of Raspbian by default You can use: sudo pip install RPi.GPIO for Python 2 and sudo pip-3.2 install RPi.GPIO for Python 3

6

Another potential cause could be because the RPi.GPIO library has C bindings and therefore need to have python-dev installed in order to compile correctly when installing.

Make sure you've got the python-dev package installed along with the RPi.GPIO package:

sudo apt-get install python-dev python-rpi.gpio

This is the recommended approach on the Adafriuit installation guide:

https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/install-python-module-rpi-dot-gpio

1

If you are using a fresh image you don’t need to install it.

For the old version

  1. sudo apt-get update
  2. sudo apt-get install rpi.gpio
1

This worked for me on my RPi 4B. Just ensure you have the last version of RPi.GPIO but installed from pip (it was newer) than apt-get:

Remove current RPi.GPIO versions:

sudo apt-get purge python{,3}-rpi.gpio

Update pip first:

sudo -H pip2 install --upgrade pip
sudo -H pip3 install --upgrade pip

Install RPi.GPIO from updated pip:

python -m pip install RPi.GPIO
python3 -m pip install RPi.GPIO
3
  • There is NO NEED to install RPi.GPIO the latest version is included in Raspberry Pi OS and if you need to reinstall use the version in the repository. All you have achieved is resurrecting an obsolete post. Commented Jan 14, 2021 at 11:36
  • 1
    But it might be useful for people that are not running Raspberry Pi OS. In my case, I was running Ubuntu for full ROS compatibility and I needed RPi.GPIO. Commented Jan 16, 2021 at 10:43
  • @JosepClotetGinovart This sollution that you have posted resolved my problem. Thanks to you Bro Commented Nov 1, 2022 at 11:50
0

Update for 2025, with the Trixie OS. A compatibility package for the RPI-GPIO is the RPI-LGPIO. This is specifically handy for running older code. You will need to remove the original rpi-gpio library so that they don't clash.

sudo apt-get update    
sudo apt-get -y install python-rpi.lgpio

A "modern" way to handle GPIO is with gpiozero, which ports with Trixie, although this might not be for everyone, but it does have some functionality to make it faster for new-comers.

I found this post useful.

2
  • 1
    This IS NOT a "replacement for the RPi.GPIO" it is a compatibility shim which installs a subset of functionality. Commented Oct 30 at 10:40
  • Thanks, yes, this is more correct. I have amended my answer. Commented Oct 31 at 12:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.