How do people use pip modules in their addons?

I have fond at least one forum post and a github repository using pip modules in their addons. I belive I have copied enough to at least have it install, but instead I get this: The command '/bin/bash -o pipefail -c apk add --no-cache python3 py3-pip && pip3 install mppsolar' returned a non-zero code: 1 when trying to install the add on.

Here is what my Dockerfile looks like:

ARG BUILD_FROM
FROM $BUILD_FROM

ENV LANG C.UTF-8

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Install requirements for add-on
RUN \
  apk add --no-cache \
    python3 \
    py3-pip \
    && pip3 install mppsolar

WORKDIR /data

# Copy data for add-on
COPY run.sh /
RUN chmod a+x /run.sh

CMD [ "/run.sh" ]

This is the config.yaml:

name: "Solar Inverter Controller"
description:  "HASS addon based on https://github.com/jblance/mpp-solar"
version: "1.0.3"
slug: "solar_inverter_controller"
init: false
arch:
  - aarch64
  - amd64
  - armhf
  - armv7
  - i386

and run.sh but it doesn’t even get that far…

#!/usr/bin/with-contenv bashio

echo "Hello world!"
mpp-solar -h

What am I doing wrong, and how would I go about fixing it? Thank you.

Anything in the logs? That is what the other post also shows… I (at least) cannot guess oin what pre-built container your basing above on

Sorry, I meant to include it in the original message. Here is what the log looks like:

Step 2/14 : FROM $BUILD_FROM
 ---> 5dc776ee9947
Step 3/14 : ENV LANG C.UTF-8
 ---> Using cache
 ---> fe203d8aa084
Step 4/14 : SHELL ["/bin/bash", "-o", "pipefail", "-c"]
 ---> Using cache
 ---> 2f2ad8ec4cc5
Step 5/14 : RUN   apk add --no-cache     python3     py3-pip     && pip3 install mppsolar
 ---> Running in f057f2c3a9f5
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/main/aarch64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/community/aarch64/APKINDEX.tar.gz
(1/19) Installing libbz2 (1.0.8-r6)
(2/19) Installing libexpat (2.6.2-r0)
(3/19) Installing libffi (3.4.6-r0)
(4/19) Installing gdbm (1.23-r1)
(5/19) Installing mpdecimal (4.0.0-r0)
(6/19) Installing libpanelw (6.4_p20240420-r0)
(7/19) Installing sqlite-libs (3.45.3-r1)
(8/19) Installing python3 (3.12.3-r1)
(9/19) Installing python3-pycache-pyc0 (3.12.3-r1)
(10/19) Installing pyc (3.12.3-r1)
(11/19) Installing py3-setuptools-pyc (69.5.1-r0)
(12/19) Installing py3-pip-pyc (24.0-r2)
(13/19) Installing py3-parsing (3.1.2-r1)
(14/19) Installing py3-parsing-pyc (3.1.2-r1)
(15/19) Installing py3-packaging-pyc (24.0-r1)
(16/19) Installing python3-pyc (3.12.3-r1)
(17/19) Installing py3-packaging (24.0-r1)
(18/19) Installing py3-setuptools (69.5.1-r0)
(19/19) Installing py3-pip (24.0-r2)
Executing busybox-1.36.1-r28.trigger
OK: 101 MiB in 66 packages
e[91merror: externally-managed-environment
× This environment is externally managed
╰─> 
    The system-wide python installation should be maintained using the system
    package manager (apk) only.
   
    If the package in question is not packaged already (and hence installable via
    "apk add py3-somepackage"), please consider installing it inside a virtual
    environment, e.g.:
    
    python3 -m venv /path/to/venv
    . /path/to/venv/bin/activate
    pip install mypackage
  
    To exit the virtual environment, run:
   
    deactivate
   
    The virtual environment is not deleted, and can be re-entered by re-sourcing
    the activate file.
    
    To automatically manage virtual environments, consider using pipx (from the
    pipx package).

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
e[0m
 ---> Removed intermediate container f057f2c3a9f5
e[0m

Here it appears to suggest that I need a venv, but both of the examples I included don’t do that, which is that I am confused about.

There are multiple reasons why this could happen (search then net) but I still need a bit more background (am also not a specialist but willing to co-think). You seem to want to build an add-on from the mpp-solar repo, correct? The why not use that Dockerfile? If you are basing in on something else, then which image are you using to build this on … and why differ from the one used by mpp-solra repo (as this has been tested by them)

EDIT: you can imo use the same Dockerfile and add your own run.sh to it…and refer to the py package

FROM python:slim

RUN pip install mppsolar

@vingerha is corredt that you can find the reason with a easy google of your error.

This is the top google answer when googling your error message.

Click the link on the second answer.

It is basically to do with the linux base you are using which has PEP668.

You either need to change the base image (i suspect the base image from homeassistant in the github link you provided does not have PEP668)

ARG BUILD_FROM=ghcr.io/hassio-addons/base:16.0.0

or create a venv in your container to then install your python package.

Thank you for the reply, I did indeed google the issue beforehand and know that I can use a venv, I was just wandering if there is some way of not doing that since clearly other people are somehow doing it.

As you pointed out using the image without PEP668 is indeed one of the ways I thought it could work, but when I set the ARG BUILD_FROM=ghcr.io/hassio-addons/base:16.0.0 it still produced the exact same error.

Regarding the usage of python:slim, when I do that then something with the filesystem seems to break because it can’t even call an example python file because it doesn’t appear to be copied into the data working directory when I use WORKDIR /data followed by COPY . ..

I guess I will just have use a vnen then. Thank you for the information.

can you maybe MORE explain what you do, your text seems to only state what does not work but lacks a lot of data to see if there is more help.
I spent 5 mins(!) to create a add-on with the mpp-solar installed along above Dockerfile but I have no clue how to use it so that is the end for me

Anyhow, I will stop responding on ‘it does not work’ :slight_smile:

Sorry if my texts seemed a bit short, I tried to be concise and focus on the exact question I was having.

What I am trying to do is to run mpp-solar on a raspberry pi running the homeassistant operating system. I tried to just run it raw in the command line via ssh, but quickly learned that you are not supposed to do that. Then I read a forum post explaining that I can put the arbitrary code I want to run into an addon since those are basically just docker containers. However that included a disclaimer of how it is different and not like classic docker in some regards. I tried to find out how exactly it is different, but I coudn’t as there doesn’t appear to be an exhaustive list of changes.

Thus I just went on to try and use it as regular docker and figure stuff out alnong the way. And my problems started with just setting up a pip package, I looked online and found other people using them just fine. I tried some of the things those people had in their dockerfiles but none of those worked for me. Which is what brought me here.

Hope this clears it up, sorry for not telling this upfront.

So…
I made a fork of the mpp repo
Added repository.json and a addon folder and that ‘installs’ … but I have no clue what else is needed to fire it off within the container.
If you add this as a custom repo in the addons section then this 'works’meaning… it compiles but it does not do a thing as not configured

vingerha/mpp-solar: Python package to communicate to MPP Solar PIP-4048MS inverters (and similar) (github.com)

EDIT: you do NOT need to fork it, I just did this to see what it did…you only need the addon content

Have you tried

RUN rm /usr/lib/python*/EXTERNALLY-MANAGED && \
    python3 -m ensurepip && \
    pip3 install <packages>