# ModuleNotFoundError: No module named 'dolfin', if PETSC\_DIR is set

**URL:** <https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407>\
**Category:** installation\
**Created:** [October 27, 2020, 2:03pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407 "2020-10-27T14:03:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)\
**Post date:** [October 27, 2020, 2:03pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/1 "2020-10-27T14:03:46Z")

</div>

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

```auto
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](https://fenicsproject.org/download/).

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![WeiOng](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/weiong/32/1795_2.png) [@WeiOng](https://fenicsproject.discourse.group/u/WeiOng)\
**Post date:** [October 28, 2020, 2:34pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/2 "2020-10-28T14:34:50Z")

</div>

> [@BenjaminRueth](#):
>
> `from dolfin import *`

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

> import dolfin as dolfin

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [October 28, 2020, 2:37pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/3 "2020-10-28T14:37:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)\
**Post date:** [October 28, 2020, 3:12pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/4 "2020-10-28T15:12:02Z")

</div>

Thanks for the suggestion! I modified my Dockerfile:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)\
**Post date:** [October 28, 2020, 3:20pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/5 "2020-10-28T15:20:04Z")

</div>

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](https://fenics.readthedocs.io/en/latest/installation.html#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.

---

<div class="post-metadata">

**Author:** ![WeiOng](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/weiong/32/1795_2.png) [@WeiOng](https://fenicsproject.discourse.group/u/WeiOng)\
**Post date:** [October 28, 2020, 3:25pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/6 "2020-10-28T15:25:08Z")

</div>

> [@BenjaminRueth](#):
>
> ```auto
> 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'
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [October 28, 2020, 4:14pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/7 "2020-10-28T16:14:17Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![WeiOng](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/weiong/32/1795_2.png) [@WeiOng](https://fenicsproject.discourse.group/u/WeiOng)\
**Post date:** [October 28, 2020, 4:30pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/8 "2020-10-28T16:30:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)\
**Post date:** [October 28, 2020, 4:58pm UTC](https://fenicsproject.discourse.group/t/modulenotfounderror-no-module-named-dolfin-if-petsc-dir-is-set/4407/9 "2020-10-28T16:58:17Z")

</div>

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