3

I successfully built python 3.7 on my raspberry pi zero. now when I run my program using:

python3 DungeonCube.py

I get the following error:

import smbus
ModuleFoundError: No module named 'smbus'

I have searched for solutions and it seems no one has gotten this far with python 3.7 on a raspberry pi. other guides say to reinstall smbus or install smbus2 and that does not work.

dpkg-query -L python3-smbus shows:

/.
/usr
/usr/lib
/usr/lib/python3
/usr/lib/python3/dist-packages
/usr/lib/python3/dist-packages/smbus.cpython-35m-arm- 
linux-gnueabihf.so
/usr/share
/usr/share/doc
/usr/share/doc/python3-smbus
/usr/share/doc/python3-smbus/changelog.Debian.gz
/usr/share/doc/python3-smbus/changelog.gz
/usr/share/doc/python3-smbus/copyright

apt-cache show python3-smbus shows:

Package: python3-smbus
Version: 3.1.2-3
Architecture: armhf
Maintainer: Aurelien Jarno <aurel32@debian.org>
Installed-Size: 31
Depends: libc6 (>=2.4), python3 (<< 3.6), python3 (>= 
3.5~), python3:any (>= 3.0~)
Recommends: i2c-tools
Provides: python3.5-smbus
.
.
.

Any ideas how to get this working?

Garry O.

Garry Osborne
  • 87
  • 2
  • 4
  • 11

2 Answers2

5

The python3-smbus library which is installed on your system seems a little old. It works for Python 3.5 but not for Python 3.7.

You have two solutions:

  • downgrade your Python to use Python 3.5
  • choose another library: you can try smbus2 which is more uptodate.
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • 1
    Thanks Laurent, pip3 install smbus2 has gotten it working. once done I had to change the import statement to import smbus2. – Garry Osborne Sep 08 '18 at 11:44
  • 1
    And remember that if you are running RPi OS Lite, you must first install `pip3`. –  Apr 08 '21 at 07:40
0

I don't have a RPi to test this on, but when I was in HackerSpace I also built python on R-Pi. I ran into this exact same problem and found a fix that may work for you. You will need to open the config file: "sudo nano /boot/config.txt" and add the line "dtparam=i2c_arm=on" after you save it run "sudo raspi-config". Under advanced options you will find 7l2c select "Yes".

  • Thanks for the contribution. I checked the config.txt file and I already had: dtparam=i2c_arm=on. I should add that my program was working using python 3.5.3 before I built python 3.7. To verify i2C is working I ran i2cdetect -y 1 and devices on the i2c bus showed fine. – Garry Osborne Sep 08 '18 at 11:20