Diablo and Python 3


It seems like WebIOPi is using Python 3, this causes two problems when using the Diablo library:

  • The old Python 2 style print statement is no longer correct
  • It will not be able to import smbus as it is only available for Python 2.X by default

The first problem is easy to fix, it simply requires a version using the correct print statements

cd ~/diablo
wget -O Diablo3.py http://piborg.org/downloads/diablo/Diablo3.py.txt

To use this version you will need to change the import statement to

import Diablo3 as Diablo

Then you can use it normally.

The second problem is a bit more tricky, what we can do is rebuild smbus for Python 3 by making some small changes to the source:

cd ~
sudo apt-get -y install python3-dev
wget http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.0.orig.tar.bz2
tar xf i2c-tools_3.1.0.orig.tar.bz2
cd i2c-tools-3.1.0/py-smbus
mv smbusmodule.c smbusmodule.c.orig
wget -O smbusmodule.c http://piborg.org/downloads/picoborgrev/smbusmodule.c.txt
wget http://lm-sensors.org/svn/lm-sensors/tags/V2-10-8/kernel/include/i2c-dev.h
python3 setup.py build
sudo python3 setup.py install

This should make the smbus module available to Python 3, and hopefully make everything work happily.

when running these changes, at line 8 the link to lm-sensors.org seems to time out, I have found another ver of the file on GitHub, I am at work right now so can't test it out, but I assume it will work,
file is here.

I wonder if this is also the reason I am having problems with my PS4 controller


After many hours of scratching everything I have, the issue was resolved with 1 line

sudo apt-get install python3-smbus



piborg's picture

Glad to hear you found a solution :)

We are planning to make our examples work with both Python 2 and 3 in the near future. When we do we will also update the install scripts to setup both Python 2 and Python 3 correctly. This should remove the need to make changes to the scripts for those who prefer to use Python 3.

brilliant, let me know if I can help, im going to delete everything over the weekend and repeat what I have done to make sure I have got it right, and ill document each step for myself... remembering I have no Idea what I am doing

I have tried using the link to the tar file (http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.0.or...) however, it seems the link is broken. (404) error. Has the link been updated or is there another way to get the smbus to work with python3

piborg's picture

Have you tried the alternative shared by jasontimothyjones above?

Yes I tried that and that threw an error as well saying something like IOError on smbus.SMbus

piborg's picture

If you have the smbus module already compiling it from source is unnecessary and will not help.

Can you re-run the script and find out what the exact error message is? A screenshot will be fine :)

Here is the screenshot of the error we get after using the smbus module obtained from sudo apt-get install python3-smbus via the diablo3 library on this page. (We have tested with python2 and it works)

piborg's picture

The problem is fairly early on in the script:

Found a dvice at 25, but it is not a Diablo (ID 00 instead of 37)

This means it can see the Diablo, but it is reading back the wrong values from the board.

I have attached a new version of the Diablo library we have been working on below that does not use the SMBus library at all. If you can download it and see if things start working correctly.

The import code will now be:

import Diablo_py3 as Diablo

You can download the new script to your Pi directly with these commands:

cd ~/diablo
wget -O Diablo_py3.py http://forum.piborg.org/sites/default/files/forum_upload/piborg/Diablo_py3.txt

Thank you that works perfectly

Subscribe to Comments for "Diablo and Python 3"