How to install WiringPi2 for Python on the Raspberry Pi

 

WiringPi2 for Python – installation instructions for Raspbian

What is WiringPi2 and why do I need it?

WiringPi2 for Python is an excellent GPIO handling system written by Gordon ‘Drogon’ Henderson and packaged for Python by Phil ‘Gadgetoid’ Howard. It uses Arduino-like scripting language. For simple GPIO work, I use RPi.GPIO because that’s what I first got started with.

But, aside from hardware PWM, one thing I love about WiringPi2 is that it also has some extra built-in drivers for various chips – e.g. the MCP23x17 & x8 port expanders. This makes handling expanded ports as easy as…

  1. defining a pin
  2. setting it as input or output
  3. reading or writing to the pin

All the low level heavy lifting is done for us. :) You’ve still got to hook up your own wires though. I think Gordon’s working on that for V3 :p

Installing WiringPi2 for Python

Installing WiringPi2 for Python is very simple, but there are a couple of required packages, so we’ll install those first. Before we install anything, though, let’s update the package lists…

sudo apt-get update
This is what should happen…

Update Raspbian package lists

Now we need the following. If they’re already installed, no harm will be done…
sudo apt-get install python-dev python-pip

If asked, confirm that you want to go ahead with the installation with y

install python-pip & python-dev

This will take several minutes. When it’s finished, you should see this…

Dependencies installed

Now we’re ready to install WiringPi2 for Python itself
sudo pip install wiringpi2

You will see lots of messages. pip, the python package installer that you just installed, is compiling WiringPi2 and installing it all for you. After lots of messages starting with “gcc – pthread”, you should see this…

WiringPi2 installed :)

Let’s check it

Now you should be good to go. Let’s check that it works with a live Python environment

sudo python
import wiringpi2
wiringpi2.piBoardRev()

Checking WiringPi2 python works

We just imported wiringpi2 and used one of its new functions piBoardRev() which is the built-in Raspberry Pi revision checker. Notice it returned a 2, which is good because I’m using a Rev 2 board. ;) If that all works as it should…

ctrl+d to exit python

You are installed and ready for action with WiringPi2’s GPIO features.

I will be creating a page for simple WiringPi2 usage instructions in the near future.

 Posted by at 2:55 pm

  37 Responses to “How to install WiringPi2 for Python on the Raspberry Pi”

  1. All loaded, tests complete inc C and Bash, but
    the following does not work:-

    wiringpi2.wiringPiSetup and SYS and Gpio (or any mix of the three)
    wiringpi2.pinMode(1,1)
    wiringpi2.digitalWrite(1,1)

    wiringpi2.delay/PiBoard etc does work.
    GPIO write 1 1 works but not the above.
    I’m going slightly mad over this!
    Can you help?

  2. I’m having a bit of a problem. I have successfully installed wiringpi2. After importing wiringpi2 in python, the wiringPiSetup( ) function, regardless of the scheme I choose to initialize, causes the python shell to restart. Any insight into a possible cause/solution would be great. Thank you!

  3. Using wiringpi2.piBoardRev() to verify the installation is not complete in that wiringpi2.wiringPiSetup() can still cause a RESTART if the permissions are not correct. A permissions issue can be checked by using the LXTerminal and at the command line start IDLE3 with the command ‘sudo IDLE3’. If the module containing wiringpi2.wiringpi2.wiringPiSetup() runs without RESTART then permissions is the issue.

  4. I just wanted to say THANK YOU FOR YOUR SUPPORT IN HELPING ME RESOLVE A MAJOR PROBLEMO. I WISH YOU WELL AND THE BEST THAT THIS WORLD HAS TO OFFER. YOUR FRIEND MICHAEL BONG FROM MILWAUKEE, WI.

  5. Is there an update to make this work with A+ additional gpio?
    Things attached to my gpio ports on the physical pins between 27-40 are not responding when using python version of wiringpi2, however if I use “gpio write” and “gpio mode” work fine on the command line.

  6. this works:

    #!/usr/bin/env python
    
    import wiringpi2 as GPIO
    
    pin = 0
    
    try:
        GPIO.wiringPiSetup()
    
        pin = int(raw_input('Enter Pin to check (0): '))
        GPIO.pinMode(pin, GPIO.GPIO.OUTPUT)
    
        print('Pin {} is going HIGH'.format(pin))
        GPIO.digitalWrite(pin, GPIO.GPIO.HIGH)
    
        raw_input('press enter when ready: ')
    
        print('Pin {} is going LOW'.format(pin))
        GPIO.digitalWrite(pin, GPIO.GPIO.LOW)
    
    finally:
        print('no cleanup to do, exiting.')
  7. getting some errors which end with
    ImportError: No module named pygments.lexer

    Any ideas on what I am actually missing or how to start debugging?I get similar errors when I try from easy_install or pip or trying to install from git version

    Traceback (most recent call last):
    File “/usr/bin/pip”, line 5, in
    from pkg_resources import load_entry_point
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 2727, in
    add_activation_listener(lambda dist: dist.activate())
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 700, in subscribe
    callback(dist)
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 2727, in
    add_activation_listener(lambda dist: dist.activate())
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 2230, in activate
    map(declare_namespace, self._get_metadata(‘namespace_packages.txt’))
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 1827, in declare_namespace
    _handle_ns(packageName, path_item)
    File “/usr/lib/python2.7/dist-packages/pkg_resources.py”, line 1797, in _handle_ns
    loader.load_module(packageName); module.__path__ = path
    File “/usr/lib/python2.7/pkgutil.py”, line 246, in load_module
    mod = imp.load_module(fullname, self.file, self.filename, self.etc)
    File “/usr/lib/pymodules/python2.7/mpl_toolkits/__init__.py”, line 3, in
    except ImportError:
    File “/usr/lib/pymodules/python2.7/mpl_toolkits/axes_grid1/__init__.py”, line 2, in
    from axes_divider import Divider, SubplotDivider, LocatableAxes, \
    File “/usr/lib/pymodules/python2.7/mpl_toolkits/axes_grid1/axes_divider.py”, line 14, in
    import matplotlib.transforms as mtransforms
    File “/usr/lib/pymodules/python2.7/matplotlib/__init__.py”, line 134, in
    from matplotlib.rcsetup import (defaultParams,
    File “/usr/lib/pymodules/python2.7/matplotlib/rcsetup.py”, line 15, in

    • Partially fixed once I realised that the missing package was called python-pygments and not just pygments. also needed python-sphinx.
      Now issue is

      import matplotlib.transforms as mtransforms
      File “/usr/lib/pymodules/python2.7/matplotlib/__init__.py”, line 134, in
      from matplotlib.rcsetup import (defaultParams,
      ImportError: cannot import name defaultParams

      • removed matplotlib with purge, then installed it again and everything now seems to work (sudo apt-get –purge remove python-matplotlib)

  8. […] For wiringpi, see https://pypi.python.org/pypi/wiringpi2 — and beginners install guide, see https://raspi.tv/how-to-install-wiringpi2-for-python-on-the-raspberry-pi#install .. This is an example of it’s general use in […]

  9. Thanks for this page. After so many googling and researches I found this site which helped me to install wringpi2. I want to use this wiringpi2 to get GPIO_GCLK working.

  10. Hi all works until:
    Sudo pip install wiringpi2

    I get:
    Cannot fetch index base URL http://pypi.python.org/simple/
    Cannot find any downloads that satisfy the requirement wiringpi2
    No distributions at all found for wiringpi2

    Is something down somewhere?

    Thanks

  11. I typed in a simple Python example to test the pins. It works fine from cli, but when I open in Python Gui I get an error saying ImportError: no module named wiringPi2

  12. Doesn’t work with python3.
    Import error, wiringpi2 not found :(
    OK with python2

  13. The “sudo pip install wiringpi2” gives a lot of errors on the new Raspian-Jessie.
    It does not work (the check at the end)

  14. Every thing seemed to load OK, but when I try to import wiringpi2 I get no module named wiringpi2 I am using a PiZero

  15. My wiringpi2 doesn’t work. Is wiringpi2 compatible with python 3??
    The issue:
    Python 3.4.2 (default, Oct 19 2014, 13:31:11)
    [GCC 4.9.1] on linux
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import wiringpi2
    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python3.4/dist-packages/wiringpi2-1.1.1-py3.4-linux-armv7l.egg/wiringpi2.py”, line 28, in
    _wiringpi2 = swig_import_helper()
    File “/usr/local/lib/python3.4/dist-packages/wiringpi2-1.1.1-py3.4-linux-armv7l.egg/wiringpi2.py”, line 24, in swig_import_helper
    _mod = imp.load_module(‘_wiringpi2’, fp, pathname, description)
    File “/usr/lib/python3.4/imp.py”, line 243, in load_module
    return load_dynamic(name, filename, file)
    ImportError: /usr/local/lib/python3.4/dist-packages/wiringpi2-1.1.1-py3.4-linux-armv7l.egg/_wiringpi2.cpython-34m.so: undefined symbol: PyString_FromStringAndSize

    Can someone help,please……

    • Yep – wiringpi python changed recently.

      Change all references from wiringpi2 to wiringpi and it should work. This was discovered yesterday and a solution is in the works so as not to break backward compatibility. The instructions will be updated to reflect this.

  16. Any update on using Python3 with wiringPi? I have been struggling for a couple of days with getting a simple I/O program running under Python3. Just ran it under Python2.7 and it successfully ran first time.

  17. I followed the instructions and it installed correctly. When I run python from the command prompt using the ‘sudo python’ command, I can import the wiringpi package (importing wringpi2 apparently doesn’t work anymore, you have to import ‘wiringpi’ with python 3). However, when I run Python (3) IDLE from the start menu and try to import wiringpi, it can’t locate any package called wiringpi or wiringpi2. It is as if the installation never happened.

  18. Thank you. Was sure banging my head as to why this was not working.

  19. Thank you for this tutorial. But I have a question
    At the beginninig I got an error when I’ve tried to install wiringpi, so I’ve tried this command
    sudo pip install wiringpi2 –upgrade an this is normally fixed the problem. Then I’ve checked that the package was sucessfully installed in the python environment and I got the same result as u.
    But when I wanted to import wiringpi in the python script. I got this error” No module named wiringpi”
    What could be the problem?

Leave a Reply