djdan_23
Posts: 19
Joined: Thu Aug 17, 2017 1:42 am

No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 12:39 pm

Hi! all.
I have Raspberry pi3 with 32gb Sd Card.
Linux Kernel=4.9.41-v7+ #1026.
Firmware=Aug 8 2017 12:01:57 Copyright 2012 Broadcom.
Python=3.6.2 (Build last week) using http://bohdan-danishevsky.blogspot.ca/2 ... y-pi-3.htm
Raspbian GNU/Linux 9 (stretch).
I use: sudo apt-get update then sudo apt-get upgrade
then sudo apt-get install python3-smbus
that write: Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-smbus is already the newest version (3.1.2-3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgrade
Everything works properly.

I have hooked-up a Chronodot (RTC) on i2c bus. When I use i2cset.... or i2cget.... in terminal,
it work normally.
I've been told that I can't use those command in a scrip. So I have a scrip
that have only one line: import smbus

In version 2.7.13
When i use: python chronodot.py it pass thru OK.
In version 3.6.2
When i use: python3.6 chronodot.py it print:

Traceback (most recent call last):
File "chronodotz.py", line 1, in <module>
import smbus
ModuleNotFoundError: No module named 'smbus'


Could anyone tell me what is wrong. Thanks

User avatar
paddyg
Posts: 2243
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 12:59 pm

viewtopic.php?f=32&t=122260 might be relevant (they seem to say you need to pip3 install it)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

djdan_23
Posts: 19
Joined: Thu Aug 17, 2017 1:42 am

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 1:13 pm

Thanks. Here<s what I did.

$ sudo apt-get install python3-smbus
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-smbus is already the newest version (3.1.2-3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

$ sudo aptitude search smbus
i python-smbus - Python bindings for Linux SMBus access thr
v python2.7-smbus -
i python3-smbus - Python 3 bindings for Linux SMBus access t
v python3.5-smbus -


$ sudo pip install smbus
Collecting smbus
Could not find a version that satisfies the requirement smbus (from versions: )
No matching distribution found for smbus

$ sudo pip3 install smbus
Collecting smbus
Could not find a version that satisfies the requirement smbus (from versions: )
No matching distribution found for smbus

The "could not find......." is in RED color

User avatar
paddyg
Posts: 2243
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 1:19 pm

and did smbus2 (as per the bottom of the thread) not work?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

djdan_23
Posts: 19
Joined: Thu Aug 17, 2017 1:42 am

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 1:38 pm

Here's the result

sudo pip install smbus2
Collecting smbus2
Downloading smbus2-0.2.0.tar.gz
Building wheels for collected packages: smbus2
Running setup.py bdist_wheel for smbus2 ... done
Stored in directory: /root/.cache/pip/wheels/90/71/b4/9f90d8e2d0349ab55fef07169a81bd8f925965f16174e2f809
Successfully built smbus2
Installing collected packages: smbus2
Successfully installed smbus2-0.2.0

python3.6 chronodot.py
Traceback (most recent call last):
File "chronodot.py", line 1, in <module>
import smbus2 as smbus
ModuleNotFoundError: No module named 'smbus2'

B.Goode
Posts: 6544
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 2:26 pm

When you install with pip you make the package available to Python2.

To make the package available to Python3 it is necessary to use pip3.

(Even that knowledge might not resolve the conflict of potentially having 2 implementations of Python3 on the system. And I acknowledge that @paddyg has said the same thing... )

Have you tried using the library on a standard Raspbian system with only the default Python3 interpreter which comes pre-installed?

djdan_23
Posts: 19
Joined: Thu Aug 17, 2017 1:42 am

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 3:05 pm

As reply at "Sun Sep 10, 2017 1:13 pm" both "pip install smbus" and "pip3 install smbus" give the same error:
Could not find a version that satisfies the requirement smbus (from versions: )
No matching distribution found for smbus
Here's the pip version installed:
# pip --version
# pip 9.0.1 from /usr/lib/python2.7/dist-packages (python2.7)

# pip3 --version
# pip 9.0.1 from /usr/local/lib/python3.6/sites-packages
# (python3.5)
I tried "python 3.0 --version" and 3.1-3.2-3.3-3.4 and it command not found
but 3.5 wrote: Python3.5.3
So i tried python3.5 chronodot.py and still the same : ImportError: No module named 'smbus2'
My scrip is one line: import smbus2 as smbus

new scrip 2lines:
import smbus as smbus
import smbus2 as smbus
and with "python3.5 chronodot.py" it pass over smbus as smbus but failed with smbus2.
and with "python3.6 chronodot.py" it failed with smbus

User avatar
rpdom
Posts: 12949
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 3:43 pm

sudo pip3 install smbus2

djdan_23
Posts: 19
Joined: Thu Aug 17, 2017 1:42 am

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 4:05 pm

YOU'RE THE BEST.
It works properly now.

Question 1 : Is smbus2 better then smbus.
Question 2 : Must I keep smbus2 instead of smbus.

Thank you very much.

User avatar
paddyg
Posts: 2243
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 5:20 pm

IMHO the python2v3 and pip2v3 ambiguity is an unasked for complication. At this stage (stretch) "python" and "pip" as symbolic links to python2.7 should have been removed so users have to type python2, python3, pip2 or pip3. At the next issue of debian (buster) python and pip should point to python3.x

It's now only 27 months until python2 is officially retired!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 5:41 pm

B.Goode wrote:
Sun Sep 10, 2017 2:26 pm
Even that knowledge might not resolve the conflict of potentially having 2 implementations of Python3 on the system.
After I had manually built and installed 3.6.0 under Jessie alongside the default 3.4 ( or whatever ) I ran into all sorts of problems when installing Python 3 packages and trying to run the AIY project.

Each Python 3 version saw different packages and things went horribly wrong. I suspect one might have to use the specific version of pip for it to use the correct path, different things seeing different defaults.

Never resolved the issue, moved on to something else, and am now on default Stretch installs.

User avatar
bensimmo
Posts: 3479
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: No module named 'smbus' on Python3.6.2

Sun Sep 10, 2017 8:11 pm

You should be able to use pip3.4 pip3.5 pip3.6 to tell it which version of python3 you want to install it too
so
pip3.6 install smbus
should do the job.
*

smbus is python3 compatible, smbus2 should no longer be needed.
I know as converted all my smbus2 i2c's back to smbus. (in raspbian at some point it wasn't and then changed, didn't notice when but sometime in jessie iirc, either than or smbus was broken on my setup ;-))


*smbus isn't on pypy iirc.
maybe you need to set some environment variables up.
Cannot help as I've never needed to do it.

jamargevicius
Posts: 6
Joined: Sun Oct 11, 2015 1:50 am

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 5:34 am

I've loaded 2 SD cards with Stretch-lite - one I did several months back, and "fiddled with apt-get installs" to finally get the I2C working. Now I wanted a clean reinstall, so on the 2nd card, I methodically loaded things as shown below. However, a simple program that just calls "import smbus2" fails in the 2nd card, but works fine in the 1st. the result of sudo pip3 install smbus2 was successful, which is perplexing, since it says "no module named smbus2" in this 2nd card. I have explored files and queries in both cards, and haven't found any differences. For example, the same for: /etc/modules/, sudo aptitude search smbus, dpkg --get-selections |grep smbus, dpkg --get-selectons |grep smbus2 ---- same thing!

Any suggestions of places I could look to see something different and explain this "no module named smbus2".

code:
# simple_smbus2_test.py
import smbus2
from smbus2 import SMBus
....

this same code works in 1 card, but not the other --- I believe they should be identical

(things done to 2nd card to bring up smbus and i2c:
raspi-config and enable I2C in the interface options
sudo apt-get install python3-smbus i2c-tools python3-setuptools python3-pip
sudo pip3 install smbus2 (states installs correctly)
)
Thanks for any help.

User avatar
paddyg
Posts: 2243
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 8:33 am

Assuming that you are using `$ python3 simple_smbus2_test.py` on both and `/usr/local/lib/..*../dist-packages/` is the same on both then it's not obvious. Once or twice I've had a missing module error message that was solved by deleting all the *.pyc files
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
bensimmo
Posts: 3479
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 10:58 am

If they are clean installed you are using 3.5 not 3.6

There is no need to be using smbus2 with (so no need to install it) switch then all to smbus.

jamargevicius
Posts: 6
Joined: Sun Oct 11, 2015 1:50 am

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 4:04 pm

Thank you both. .... wierd-er still.

old card: python simple_smbus2_test.py works fine. python3 simple_smbus2_test.py doesn't ("no module....")
in /usr/local/lib, there is pypy2.7, python2.7 and python 3.5

new card: python simple_smbus2_test.py doesn't work, but python3 simple_smbus2_test.py does !
in /usr/local/lib, there is only python2.7 and python 3.5

The good news: it works now (with python3). Thanks for the help.

User avatar
paddyg
Posts: 2243
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 9:41 pm

Well that doesn't sound too weired at all. You almost certainly didn't use pip3 on your old card.

I haven't needed to use smbus but I would heed the advice of a 'regular' like @bensimmo
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Idahowalker
Posts: 403
Joined: Wed Jan 03, 2018 5:43 pm

Re: No module named 'smbus' on Python3.6.2

Fri Oct 05, 2018 9:47 pm

djdan_23 wrote:
Sun Sep 10, 2017 12:39 pm
Could anyone tell me what is wrong. Thanks
Just for fun, can you put

Code: Select all

import serial
as the top entry and do you get a no module named serial found when you try to run the code?
Without knowing why you are deleting my postings, I will not know how...

jamargevicius
Posts: 6
Joined: Sun Oct 11, 2015 1:50 am

Re: No module named 'smbus' on Python3.6.2

Sat Oct 06, 2018 6:16 am

I rebuilt the card, and now only use smbus (not smbus2) in the code. It works fine, but I had to install python3-RPi.GPIO for my "real" program, since it complained that no such module existed (not the case with the "old" card - worked fine). Anyway, now have a "cleaner" install to release. Thank you.

Here is what I added after stretch-lite was brought up:
1) sudo raspi-config, and turned on I2C in the Interface options
2) sudo apt-get install python3-smbus
3) sudo apt-get install i2c-tools
(at this point, it complained that "no module name RPi" in code that uses the RPi I/O)
4) sudo apt-get dist-upgrade (a blogger suggested this; it didn't solve the "no module RPi" problem however, but changed dates in /usr/local/lib)
5) sudo apt-get install python3-RPi.GPIO (this solved the RPi problem, and the code now runs as in the "old" card).

contents of /usr/local/lib: python2.7 (dated today!), and python3.5 dated Nov 28, 2017)

(FYI, I tried putting "import serial" in the code, but it announced the "no module named serial" error)

Thanks again for the help. :)

Return to “Python”