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…
- defining a pin
- setting it as input or output
- 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…
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
This will take several minutes. When it’s finished, you should see this…
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…
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()
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.
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?
Travelling back to UK today. Will have a look when I get back. Sorry for the delay.
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!
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.
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.
Thats a very nice comment u left in here sir
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.
I think the C version is updated, but not sure about Python.
this works:
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)
[…] 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 […]
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.
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
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
No idea, sorry. I don’t use a Python GUI at all. Was it IDLE? Most GPIO libraries require root priviliges. I wonder if it’s something to do with that?
need to import wiringpi2 (note the p is lowercase).
yep that’d be it. Good catch. I missed it.
Doesn’t work with python3.
Import error, wiringpi2 not found :(
OK with python2
The “sudo pip install wiringpi2” gives a lot of errors on the new Raspian-Jessie.
It does not work (the check at the end)
Forget my message. Retried it with a fresh image and IT WORKS, thanks
Every thing seemed to load OK, but when I try to import wiringpi2 I get no module named wiringpi2 I am using a PiZero
It does not work on my Pi2 either, I get the same message. I rebooted and that did not help
I think there were some changes recently. What happens if you just remove the 2?
Does it work now? If so, I’ll update the instructions.
It worked great, Thanks for your help and fast response.
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.
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.
By thew way – on RPi3B, fully updated. The installation seemed to go OK. I can run the board rev test above ok, but had found that when running under Python3 Idle, I had to import wiringPi (no ‘2’).
Did the same in my I/O program. Under Python3 it seems to restart a couple of times, as if it locks up when a wiringPi call is made. As I said, runs perfectly under Python2.7.
I don’t use wiringPi myself, but does https://github.com/WiringPi/WiringPi-Python/issues/33 help?
If not, https://github.com/WiringPi/WiringPi-Python/issues is the best place to report problems, rather than the raspi.tv blog ;-)
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.
Did anyone find a solution to this. Even i am facing what ChrisG is facing.
Thank you. Was sure banging my head as to why this was not working.
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?
As I mentioned earlier, you’ll be much better off asking for help at https://github.com/WiringPi/WiringPi-Python than you are by posting comments on a 6-year-old blog article on RasPi.tv ;-)
I already found a solution. Thanks for ur help