Using libraries, can't find errors

Hi all, New to Balena

I’m trying to setup a Pi3 to use a I2C device and CANBUS libraries however my python scripts just crash with ImportError: No module named XXXX on lines import smbus or import can.

I’ve tried installing them in the Dockerfile, the requirements.txt file and even tried using different base images but no luck, all I2C example projects are older and still using resin code.

Here is my Dockerfile.template file, modified from the getting started example.

# base-image for python on any machine using a template variable,
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/
#FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
FROM balenalib/raspberrypi3-debian-python:latest # Base OS doesn’t seem to make any difference

# use 'install_packages' if you need to install dependencies,
RUN install_packages apt-utils
RUN install_packages i2c-tools
RUN install_packages python-smbus

# Set our working directory
WORKDIR /usr/src/app

# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt #Currently empty

# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt

# This will copy all files in our root to the working directory in the container
COPY . ./

# Enable udevd so that plugged dynamic hardware devices show up in our container.
#ENV UDEV=1

# CMD will run when container starts up on the device
CMD ["python","-u","app/HelloWorld.py"] # “import smbus” inside script, throws error

Hi @linuxnoob welcome to the forums! I think you need to install the python3-smbus package instead as the python-smbus package is for python 2 instead of version 3. I was able to run a simple script that imported smbus and can using the following Dockerfile.template:

# base-image for python on any machine using a template variable,
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run

# use 'install_packages' if you need to install dependencies,
RUN install_packages apt-utils
RUN install_packages i2c-tools
RUN install_packages python3-smbus
RUN install_packages python3-can

# Set our working directory
WORKDIR /usr/src/app

# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt

# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt

# This will copy all files in our root to the working directory in the container
COPY . ./

# Enable udevd so that plugged dynamic hardware devices show up in our container.
#ENV UDEV=1

# CMD will run when container starts up on the device
CMD ["python","-u","app/HelloWorld.py"]