ModuleNotFoundError: No module named 'dolfin', if PETSC_DIR is set

Hi,
since a few weeks ago I’m facing the following error message:

Step 13/13 : RUN python3 -c "from fenics import *"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/fenics/__init__.py", line 7, in <module>
    from dolfin import *
ModuleNotFoundError: No module named 'dolfin'

I installed FEniCS using apt following the instructions on the website.

After a few more experiments I found out that my PETSc installation (from source) seems to interfere with the FEniCS installation. If PETSC_DIR is set, dolfin cannot be found. Everything works nicely, if PETSc is not installed.

Am I doing something wrong with PETSc? Or did something break recently with the FEniCS installation from apt? Note that the FEniCS docker image quay.io/fenicsproject/stable:current does not make any problems, if PETSc is installed on top.

Here is the Dockerfile that I used for my experiments:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y -qq software-properties-common git
RUN add-apt-repository ppa:fenics-packages/fenics
RUN apt-get update
RUN apt-get install -y --no-install-recommends fenics
RUN python3 -c "from fenics import *"

# Installing necessary dependencies
RUN apt-get -qq update && apt-get -qq install \
    build-essential \
    wget \
    bzip2 \
    cmake && \
    rm -rf /var/lib/apt/lists/*

RUN python3 -c "from fenics import *"

# Setting some environment variables for installing PETSc
ENV PETSC_DIR="/petsc/petsc-3.12.5" \
    PETSC_ARCH="arch-linux2-c-debug" \
    LD_LIBRARY_PATH="/petsc/petsc-3.12.5/arch-linux2-c-debug/lib:${LD_LIBRARY_PATH}" \
    PKG_CONFIG_PATH="/petsc/petsc-3.12.5/arch-linux2-c-debug/lib/pkgconfig:${PKG_CONFIG_PATH}"

# Install PETSc from source
WORKDIR /petsc
RUN wget -nv 'https://www.mcs.anl.gov/petsc/mirror/release-snapshots/petsc-3.12.5.tar.gz' -O - | tar xz && \
  cd petsc-3.12.5 && \
  ./configure && \
  make all test

# the following line will raise the error
RUN python3 -c "from fenics import *"

Thanks for any help!
Benjamin

If I remember it correctly, I fix the error by adding one more line

import dolfin as dolfin

Another possibility: Dolfin is built against PETSc, so it needs the version of PETSc that it was built against. If you set PETSC_DIR to a different version, then there won’t be a dolfin build to match, hence "ModuleNotFoundError: No module named ‘dolfin’". You might want to rebuild dolfin against the custom version of PETSc that you want to use.

1 Like

Thanks for the suggestion! I modified my Dockerfile:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y -qq software-properties-common git
RUN add-apt-repository ppa:fenics-packages/fenics
RUN apt-get update
RUN apt-get install -y --no-install-recommends fenics
RUN python3 -c "from fenics import *"

# Installing necessary dependencies
RUN apt-get -qq update && apt-get -qq install \
    build-essential \
    wget \
    bzip2 \
    cmake && \
    rm -rf /var/lib/apt/lists/*

RUN python3 -c "from fenics import *"

# Setting some environment variables for installing PETSc
ENV PETSC_DIR="/petsc/petsc-3.12.5" \
    PETSC_ARCH="arch-linux2-c-debug" \
    LD_LIBRARY_PATH="/petsc/petsc-3.12.5/arch-linux2-c-debug/lib:${LD_LIBRARY_PATH}" \
    PKG_CONFIG_PATH="/petsc/petsc-3.12.5/arch-linux2-c-debug/lib/pkgconfig:${PKG_CONFIG_PATH}"

# Install PETSc from source
WORKDIR /petsc
RUN wget -nv 'https://www.mcs.anl.gov/petsc/mirror/release-snapshots/petsc-3.12.5.tar.gz' -O - | tar xz && \
  cd petsc-3.12.5 && \
  ./configure && \
  make all test

# suggested by WeiOng
RUN python3 -c "import dolfin as dolfin; from fenics import *"

But I unfortunately still get an error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'dolfin'

Thanks for the explanation. To make sure that I understand it correctly:

  • If I want to use a specific PETSc version (i.e. 3.12.5) this will not allow me to use apt for installation anymore, but I will have to install FEniCS from source.
  • If I use the “right” PETSc version or set PETSC_DIR to point to the PETSc that is installed when calling apt-get install fenics I don’t have to build FEniCS from source.

Sorry, I think you misunderstand what I mean, In your original post,

Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python3/dist-packages/fenics/init.py”, line 7, in
from dolfin import *
ModuleNotFoundError: No module named ‘dolfin’

so what I suggest is to modify init.py by adding one more line, so the first few lines will looks like these

from dolfin import *
import dolfin as dolfin

That import dolfin as dolfin doesn’t look right to me. The error here seems to be simply that the dolfin module isn’t there. I don’t think it’ll start being there simply by importing it as itself. Putting it in fenic module’s __init__.py will make it known under the fenics module, but only if it exists.

@BenjaminRueth, apart from the import dolfin line, you’ve got the right sense of it. The Ubuntu (and Debian) packages are built to be self-consistent, which is why apt-get install fenics “just works”. Ubuntu 18.04 only provides PETSc 3.7, so it’s reasonable for you to want to install a more recent PETSc, though you might make life easier by upgrading Ubuntu itself rather than PETSc (Ubuntu 20.04 provides PETSc 3.12.4).

Some more background on what’s going on with PETSC_DIR and the dolfin module: partly to help make these PETSc version dependencies more manageable, the dolfin python module is not actually installed in the python path. It’s installed under the PETSC_DIR that it was built against (that’s why PETSC_DIR needs to match). Instead, dolfin.pth is installed in the python module. .pth files are a python trick for manipulating the known modules. In this case dolfin.pth adds the dolfin module located under PETSC_DIR, if PETSC_DIR is defined. If it’s not set then the default petsc at /usr/lib/petsc is used.

So if you want to use a custom PETSC_DIR, you need a custom dolfin build placed under it (the simplest way to do that might be to grab the Ubuntu or Debian dolfin source, and rebuild with dpkg-buildpackage after setting the required value of PETSC_DIR in debian/rules)

You’re doing this in docker which complicates things further (or simplifies them, depending on your perspective). Docker is self-contained so can facilitate alternative self-consistent builds. But using apt inside docker means you’re mixing the metaphors. That is, apt-get will want to impose the Ubuntu/Debian self-consistency of packages inside the docker container. You’d want to do that if you don’t have administrator rights on your machine. Otherwise, just use apt without docker (you don’t need docker, if you’re happy with the versions provisioned for Ubuntu).

4 Likes

Thanks @dparsons for your sharing, yup, I think I overlook it. Thanks for the correction.

1 Like

@dparsons also from my side: thanks a lot for the help and the great explanation :pray:

1 Like