Installing packages with package manager in Balena images

I am trying to install PyICU in to a Balena container. The container I am using is:

balenalib/%%BALENA_ARCH%%-debian-python:3.8.10-buster-20210506

The container already has Python installed in to /usr/local/bin/python3:

# which python3
/usr/local/bin/python3

I then run the install command for PyICU:

install_packages python3-icu

Which gives the following output:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libexpat1 libicu63 libmpdec2 libpython3-stdlib libpython3.7-minimal libpython3.7-stdlib mime-support python3
  python3-minimal python3.7 python3.7-minimal
Suggested packages:
  python3-doc python3-tk python3-venv python3.7-venv python3.7-doc binutils binfmt-support
Recommended packages:
  bzip2 file xz-utils
The following NEW packages will be installed:
  libexpat1 libicu63 libmpdec2 libpython3-stdlib libpython3.7-minimal libpython3.7-stdlib mime-support python3 python3-icu
  python3-minimal python3.7 python3.7-minimal
0 upgraded, 12 newly installed, 0 to remove and 27 not upgraded.
Need to get 12.7 MB of archives.

Because Balena images have Python installed in /usr/local/bin/ rather than /usr/bin, it is fetching Python again and installing it again through the package manager. Of course I would like to try and avoid this as it is adding extra size to my images. Any ideas how? Ideally without having to build PyICU or other packages from source.

Hi there, you should set up a python venv instead and use pip to install the library/module (e.g. pip install PyICU). this is a good article describing the correct way to setup venv at build time: Elegantly activating a virtualenv in a Dockerfile

PyICU from PIP doesn’t install all the dependencies. Some are not Python related. So in the end I had to install each dependency separately, rather than use the combined python3-ICU package, then use PIP for the PyICU. It would have been easier to of been able to use the combined package of course, but it did the trick.

Thanks for following up.