Docker dolfinx:v0.9.0 error "module '_distutils_hack' has no attribute 'add_shim'"

Hello!

When importing numpy (and other libraries) in dolfinx/dolfinx:v0.9.0 on one of my windows computers, I get

Attribute error: module '_distutils_hack' has no attribute 'add_shim'

but I don’t get the error on v0.8.0 or on my other windows computer.

Here is a MWE of the error I am getting:

I’m using docker on windows. I’m going to try reinstalling docker as it works on a different windows computer but wanted to ask if anyone else has seen this.

A simple google search has this as very first result https://stackoverflow.com/questions/70527939/attributeerror-module-distutils-hack-has-no-attribute-ensure-shim

Thank you for your response! The link says that you need to upgrade setuptools or use an older version of pip. Unfortunately, I do not think I am installing setuptools and other libraries within the docker image correctly. Is there a preferred way to install or upgrade python libraries within the docker image? I am having issues with pip install setuptools, apt install setuptools, and also python3 -m venv ..

Here is what happens when I upgrade setuptools in v0.9.0:

First I run the image interactively

PS C:\Users> docker run --rm -ti dolfinx/dolfinx:v0.9.0

and I reproduce the error

root@d3fe23b2c128:~# python3 -c "import dolfinx; import numpy; import petsc4py; import ufl"
Error processing line 1 of /usr/lib/python3/dist-packages/distutils-precedence.pth:

  Traceback (most recent call last):
    File "<frozen site>", line 201, in addpackage
    File "<string>", line 1, in <module>
  AttributeError: module '_distutils_hack' has no attribute 'add_shim'

Remainder of file ignored

So I look to change to su and upgrade setuptools

root@d3fe23b2c128:~# su
root@d3fe23b2c128:~# pip install --upgrade setuptools
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.12/README.venv for more information.

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.

No luck with the first two recommended options:

root@d3fe23b2c128:~# apt install python3-setuptools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python3-setuptools

root@d3fe23b2c128:~# python3 -m venv .
Error: Command '['/root/bin/python3', '-m', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

After subsequently running apt update && apt install pipx, I also didn’t have any luck.

So I try:

pip install --upgrade setuptools --break-system-packages

I then try to reproduce the error:

root@d3fe23b2c128:~# python3 -c "import dolfinx; import numpy; import petsc4py"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

So I repeat with numpy

pip install --upgrade numpy --break-system-packages

I also need to repeat with petsc4py but that doesn’t work.