How To Fix Error Loading RPi.GPIO Python Library On Your Brand New Raspberry Pi 2

Screenshot from 2015-02-05 15:32:17

If you’ve just setup your shiny new Raspberry Pi 2 and tried to run a Python script that accesses GPIO you might be sad to see an error like this:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
RuntimeError: This module can only be run on a Raspberry Pi!

Don’t worry!  The version of the RPi.GPIO Python library shipped with the latest Raspbian OS doesn’t support the Pi 2 out of the box, but you can install a later version to fix the problem.

First check what version of the RPi.GPIO library is currently available from Python’s package index here.  Version 0.5.9 and below unfortunately are not compatible with the Pi 2, however version 0.5.10 and above should work.

If you see version 0.5.10 or greater on the site then you can install it with a few simple commands.  First connect to your Pi 2 in an SSH / terminal session (check out this guide if you’re new to Linux & the Pi 2, or try the awesome new Pi Finder to get a terminal open easily).  Once connected to your Pi 2 run these commands to update RPi.GPIO to the latest version:

sudo apt-get update
sudo apt-get install python-pip
sudo pip install -U RPi.GPIO

Answer yes to any questions that come up about downloading and installing software.  After everything finishes you should be all set and ready to use the RPi.GPIO library on the Pi 2.  Remember this method will only work once the RPi.GPIO library on the Python package index is updated to version 0.5.10 or greater!

As of the time of this writing the 0.5.10 version hasn’t been put up on the Python package index yet, so as a workaround you can manually download the RPi.GPIO library from its home on Sourceforge and install it with a few simple commands.  Don’t worry this only takes a few more commands than installing from the Python package index.  Connect to your Pi 2 in a terminal session as mentioned above, but this time execute these commands:

sudo apt-get update
sudo apt-get install build-essential python-dev python-pip mercurial
cd ~
hg clone http://hg.code.sf.net/p/raspberry-gpio-python/code raspberry-gpio-python-code
cd raspberry-gpio-python-code
sudo python setup.py install

Answer yes to any questions about installing packages.  After running the last command above you should see some text scroll by as the library is compiled and installed.  If it successfully installs you should see a message like “Writing /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.5.10a.egg-info” as the last output.  At this point you’re all set to use the RPi.GPIO library on your Pi 2!  If you see an error message carefully check all the commands above were successfully executed and try again.

I gave the latest 0.5.10a version of the RPi.GPIO library a quick test and it seems to work great for accessing GPIO on the Pi 2.  Since the Pi 2 is brand new you might run into issues or problems with libraries like RPi.GPIO.  The best place to go for help are the Raspberry Pi forums–good luck and enjoy your Pi 2!


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Join Adafruit on Mastodon

Adafruit is on Mastodon, join in! adafruit.com/mastodon

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Join over 36,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


Maker Business — Making sure the CHIPS act isn’t just crumbs

Wearables — Don’t sweat it

Electronics — Potentiometer conventions

Python for Microcontrollers — Python on Microcontrollers Newsletter: CircuitPython 8.1.0.RC0 is out, LEGO Minecraft Blocks with Python & more! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

Adafruit IoT Monthly — AI Teddybear, Designing Accessible IoT Products, and more!

Microsoft MakeCode — MakeCode Thank You!

EYE on NPI — Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 5/24/23 Feat. Adafruit Mini I2C Gamepad with seesaw – STEMMA QT / Qwiic!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !



4 Comments

  1. RPi.GPIO should be released this weekend. It could well require a firmware fix that has only just been committed on github to work properly. Not having this fix will cause havoc with anything that uses software PWM.

  2. Awesome, thanks for the update and providing such a great library!

  3. Many thanks for providing this info, your much more than a company, at times like this your a life saver….well time at least, nearly a wasted weekend working with GPIO on the Raspberry Pi 2.

    Second the thanks to Ben for providing such a great (and essential) library, thanks also from the many kids who get such enjoyment in controlling the real world (well led’s and buzzers at least) in the school environment.

  4. Got a RPi 2 B today and did an apt-get update followed by an apt-get upgrade and it now has GPIO ver 0.5.10

    I will try some of the Adafruit libs like backpack display later
    but will first make a backup of the upgraded and somewhat set up image on the SD card just in case the libs are not yet ready for RPi 2 B

    Thanks Ben for a great lib
    Thanks Tony for all your tutorials they have been a great learning examples as I learn Python .

    Tom

Sorry, the comment form is closed at this time.