ModuleNotFoundError: No module named dolfin

Dear all,

I saw a similar question has been posted related to this topic earlier on.
However, I am not able to solve the problem with this thread.
Is anyone familiar with this problem, or knows how it could be resolved?
FENICs package is installed and recognized.
Once it is imported with; ‘from fenics import *’, this returns the error; ‘/usr/lib/python3/dist-packages/fenics/init.py’. Over here it tries to import the dolfin package.
So when using; ‘from dolfin import *’, this returns the error; ‘ModuleNotFoundError: No module named ‘dolfin’’.
If I look underneath the packages of the python interpreter I make use of in /bin/python3, fenics-dolfin is mentioned.
Already thanks in advance for your help.

Kind regards

Please add a full explaination of:

  1. how did you install dolfin
  2. what is the full trace of python3 -c "import dolfin"

I installed dolfin according to the description provided in the following link for Ubuntu (PPA); Installation — FEniCS Project documentation
When I try to import the package in VSC, it also gives the suggestion to dolfin and turns green afterwards.

With the full trace provided by you, it returns; File “”, line 1, in
ModuleNotFoundError: No module named ‘dolfin’

It seems to be installed in the directory /usr/lib/python3/dist-packages/, while the python interpreter is located at /bin/python3.
Over here the following is found, which I assume to be associated with dolfin;
dolfin.pth; dolfin_utils; dolfinx.pth; fenics; fenics_basix-0.5.1.dist-info; fenics_dijitso-2019.2.0.dev0.egg-info; fenics_dolfin-2019.2.0.dev0.egg-info; fenics_ffc-2019.2.0.dev0.egg-info; fenics_ffcx-0.5.0.dist-info; fenics_fiat-2019.2.0.dev0.egg-info; fenics_ufl-2022.2.0.dist-info; ffc; ffcx

In the dolfin.pth & dolfinx.pth it says;
import sys, os; petsc_dir = ‘/usr/lib/petsc’; petsc_dir = os.path.join(os.getenv(‘PETSC_DIR’) or petsc_dir, ‘lib/python3/dist-packages’); petsc_dir in sys.path or sys.path.append(petsc_dir);

You would have to append /usr/lib.python3/dist-packags to your python path if it is not already there when calling
echo $PYTHONPATH

Ok thanks for your help.
Maybe a silly follow-up question, but how to add or change this path?
Should this be done through; nano ~/.bashrc?

It sounds like your package installation might be inconsistent. dolfin is packaged in line with the PETSc library it was built against. Does it help if you update your packages?

sudo apt update
sudo apt upgrade
sudo apt dist-upgrade

Check if you are using the most recent ubuntu release (22.04 jammy). I strongly recommend updating to the most recent release if you’re not already using it.

Hmm kinda strange.
Added the following line to ~/.bashrc; export PYTHONPATH=${PYTHONPATH}:/usr/lib.python3/dist-packags

When I run the command echo $PYTHONPATH, it returns also :/usr/local/bin:/usr/lib/python3/dist-packages
However when trying to import the dolfin package with the python interpreter in /bin/python3 in VSC, it still returns; from dolfin import *
ModuleNotFoundError: No module named ‘dolfin’

Everything was up to date once I did
sudo apt update
sudo apt upgrade
sudo apt dist-upgrade

That should be lib/python3, it was a typo in Dokken’s post. Still, you shouldn’t have to add standard paths to PYTHONPATH.

What was the result you got from the test command dokken asked about?

1 Like

It still does not work, which seems odd.
Tried a couple of things, echo $PYTHONPATH returns; /usr/lib/python3/dist-packages
python3 -c “import dolfin” still returns; Traceback (most recent call last):
File “”, line 1, in
ModuleNotFoundError: No module named ‘dolfin’

Does it matter where the python file is stored? Right now it is in my home/username directory, so when I run the file in VSC, it starts with; /bin/python3 /home/username/test.py
Hereafter it returns the no module named dolfin error

Or could it be possible something is not readable/writable? When I try in the home directory; pip install fenics, this returns; Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: fenics in ./.local/lib/python3.9/site-packages (2019.1.0)

After the collection is completed, it says;
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from fenics-dijitso<2019.2,>=2019.1.0->fenics) (1.21.5)
Requirement already satisfied: sympy in /usr/lib/python3/dist-packages (from fenics-fiat<2019.2,>=2019.1.0->fenics) (1.9)
Installing collected packages: fenics-ufl, fenics-fiat, fenics-dijitso, fenics-ffc
WARNING: The script dijitso is installed in ‘/home/username/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts ffc and ffc-3 are installed in ‘/home/username/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
fenics-ffcx 0.5.0 requires fenics-ufl<2022.3.0,>=2022.2.0, but you have fenics-ufl 2019.1.0 which is incompatible.
Successfully installed fenics-dijitso-2019.1.0 fenics-ffc-2019.1.0.post0 fenics-fiat-2019.1.0 fenics-ufl-2019.1.0

You definitely do not want to use pip to install any fenics component. pip messes up the chain of dependencies irrevocably (the problem in particular is with the binary python extensions built for dolfin). You absolutely must eliminate any and all fenics packages located under .local if you want to use the ubuntu package system.

You referred to VSC several times, I presume you mean Microsoft’s Visual Studio C. The MS Windows version of VSC will not work of course. Even on linux I could imagine problems if it maintains its own python environment separate from the ubuntu packaging system.

Note that /bin/python3 is not the standard location of python3 on a Debian-based system.

What is the verbatim output of

$ lsb_release -a   # install the lsb-release package if necessary
$ python3 --version
$ /bin/python3 --version
$ dpkg -l *dolfin* *ffc* *ufl* *dijitso* *petsc* | cat
$ echo $PETSC_DIR

For readability, use ``` tags to encapsulate and quote the output that you copy here.

hahahaha ok I must admit something is terribly wrong over here.
Yes I’m using Microsoft Visual Studio Code on Ubuntu 22.04 LTS.
Indeed something is wrong with the way python is invoked. I see what you mean, also the PETSC directory is very different.
Is there a (easy) way to solve this?

Below are the outputs
$ lsb_release -a
LSB Version: core-11.1.0ubuntu4-noarch:printing-11.1.0ubuntu4-noarch:security-11.1.0ubuntu4-noarch Distributor ID: Ubuntu Description: Ubuntu 22.04.2 LTS Release: 22.04 Codename: jammy

$ python3 --version
Python 3.9.6

$ /bin/python3 --version
Python 3.10.6

$ dpkg -l *dolfin* *ffc* *ufl* *dijitso* *petsc* | cat
dpkg-query: no packages found matching dolfinx-0.5.2
dpkg-query: no packages found matching fenics-dolfinx_0.5.2-2build1.debian.tar.xz
dpkg-query: no packages found matching fenics-dolfinx_0.5.2.orig.tar.gz
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
++±===============================-==============================-============-================================================================
un libpetsc-complex-3.6.2-dev none none (no description available)
un libpetsc-complex-3.6.3-dev none none (no description available)
un libpetsc-complex3.15-dbg none none (no description available)
un libpetsc-complex3.15-dev none none (no description available)
ii libpetsc-real-dev 3.15.5+dfsg1-1build2 all Virtual package depending on latest PETSc development package
un libpetsc-real3.10 none none (no description available)
ii libpetsc-real3.15:amd64 3.15.5+dfsg1-1build2 amd64 Shared libraries for version 3.15 of PETSc
un libpetsc-real3.15-dbg none none (no description available)
ii libpetsc-real3.15-dev:amd64 3.15.5+dfsg1-1build2 amd64 Static libraries, shared links, header files for PETSc
un libpetsc3.10-dev-common none none (no description available)
un libpetsc3.10-dev-examples none none (no description available)
un libpetsc3.15 none none (no description available)
ii libpetsc3.15-dev-common 3.15.5+dfsg1-1build2 all Common header and support dev files for PETSc
ii libpetsc3.15-dev-examples 3.15.5+dfsg1-1build2 all Static libraries, shared links, header files for PETSc
un libpetsc3.15.5-dev none none (no description available)
un libpetsc3.6 none none (no description available)
un libpetsc3.6.2-dev none none (no description available)
un libpetsc3.6.3-dev none none (no description available)
ii petsc-dev 3.15.5+dfsg1-1build2 all Virtual package depending on latest PETSc development package
un petsc3.15-doc none none (no description available)
un python-dijitso none none (no description available)
un python-dijitso-doc none none (no description available)
un python-petsc none none (no description available)
un python-petsc4py none none (no description available)
ii python-petsc4py-doc 3.15.1-2build1 all Python bindings for PETSc libraries: documentation and examples
un python-petsc4py-docs none none (no description available)
un python-ufl none none (no description available)
ii python-ufl-doc 2022.2.0-2~ppa1~jammy1 all documentation and demos for UFL
ii python3-dijitso 2019.2.0~git20190418.c92dcb0-2 all distributed just-in-time building of shared libraries (Python 3)
ii python3-petsc4py 3.15.1-2build1 all Python 3 bindings for PETSc libraries
un python3-petsc4py-64-complex none none (no description available)
un python3-petsc4py-64-complex3.15 none none (no description available)
un dpkg-query: no packages found matching ffc
python3-petsc4py-64-real none <none (no description available)
un python3-petsc4py-64-real3.15 none none (no description available)
un python3-petsc4py-complex none none (no description available)
un python3-petsc4py-complex3.15 none none (no description available)
ii python3-petsc4py-real 3.15.1-2build1 all Python 3 bindings for PETSc libraries (real numbers)
ii python3-petsc4py-real3.15 3.15.1-2build1 amd64 Python 3 bindings for PETSc 3.15 libraries (real numbers)
ii python3-ufl 2022.2.0-2~ppa1~jammy1 all unified language for form-compilers (Python 3)

$ echo $PETSC_DIR
/u/sw/toolchains/gcc-glibc/11.2.0/pkgs/petsc/3.15.1

Please use the code quote marks (```) for the entire output to make it readable (the dpkg -l output in particular).

Sorry for the confusion. In my opinion it didn’t look very clear that way. I still struggle with changing the python base. I have also tried to change it with the following inputs, but unfortunately it did not change the python version on the system.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10
sudo update-alternatives --install bin/python python /bin/python3.10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
sudo update-alternatives --install /bin/python python /bin/python3 1

Underneath the dpkg -l output

dpkg-query: no packages found matching dolfinx-0.5.2 dpkg-query: no packages found matching fenics-dolfinx_0.5.2-2build1.debian.tar.xz dpkg-query: no packages found matching fenics-dolfinx_0.5.2.orig.tar.gz dpkg-query: no packages found matching ffc Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===============================-==============================-============-================================================================ un libpetsc-complex-3.6.2-dev <none> <none> (no description available) un libpetsc-complex-3.6.3-dev <none> <none> (no description available) un libpetsc-complex3.15-dbg <none> <none> (no description available) un libpetsc-complex3.15-dev <none> <none> (no description available) ii libpetsc-real-dev 3.15.5+dfsg1-1build2 all Virtual package depending on latest PETSc development package un libpetsc-real3.10 <none> <none> (no description available) ii libpetsc-real3.15:amd64 3.15.5+dfsg1-1build2 amd64 Shared libraries for version 3.15 of PETSc un libpetsc-real3.15-dbg <none> <none> (no description available) ii libpetsc-real3.15-dev:amd64 3.15.5+dfsg1-1build2 amd64 Static libraries, shared links, header files for PETSc un libpetsc3.10-dev-common <none> <none> (no description available) un libpetsc3.10-dev-examples <none> <none> (no description available) un libpetsc3.15 <none> <none> (no description available) ii libpetsc3.15-dev-common 3.15.5+dfsg1-1build2 all Common header and support dev files for PETSc ii libpetsc3.15-dev-examples 3.15.5+dfsg1-1build2 all Static libraries, shared links, header files for PETSc un libpetsc3.15.5-dev <none> <none> (no description available) un libpetsc3.6 <none> <none> (no description available) un libpetsc3.6.2-dev <none> <none> (no description available) un libpetsc3.6.3-dev <none> <none> (no description available) ii petsc-dev 3.15.5+dfsg1-1build2 all Virtual package depending on latest PETSc development package un petsc3.15-doc <none> <none> (no description available) un python-dijitso <none> <none> (no description available) un python-dijitso-doc <none> <none> (no description available) un python-petsc <none> <none> (no description available) un python-petsc4py <none> <none> (no description available) ii python-petsc4py-doc 3.15.1-2build1 all Python bindings for PETSc libraries: documentation and examples un python-petsc4py-docs <none> <none> (no description available) un python-ufl <none> <none> (no description available) ii python-ufl-doc 2022.2.0-2~ppa1~jammy1 all documentation and demos for UFL ii python3-dijitso 2019.2.0~git20190418.c92dcb0-2 all distributed just-in-time building of shared libraries (Python 3) ii python3-petsc4py 3.15.1-2build1 all Python 3 bindings for PETSc libraries un python3-petsc4py-64-complex <none> <none> (no description available) un python3-petsc4py-64-complex3.15 <none> <none> (no description available) un python3-petsc4py-64-real <none> <none> (no description available) un python3-petsc4py-64-real3.15 <none> <none> (no description available) un python3-petsc4py-complex <none> <none> (no description available) un python3-petsc4py-complex3.15 <none> <none> (no description available) ii python3-petsc4py-real 3.15.1-2build1 all Python 3 bindings for PETSc libraries (real numbers) ii python3-petsc4py-real3.15 3.15.1-2build1 amd64 Python 3 bindings for PETSc 3.15 libraries (real numbers) ii python3-ufl 2022.2.0-2~ppa1~jammy1 all unified language for form-compilers (Python 3)

I’m sorry to be annoying about the output formatting, but it really is important for understanding what’s wrong with your system. There’s still a problem with the display of your output since it’s lost all formatting (in particular tabbing, which is what makes it readable).

The ``` needs to be be placed on a blank line before and after the output text… It can be started with the “Preformatted text” icon </> (or Ctrl-E) in the toolbar for the edit box.

The text needs to be the output from the console where you ran the command, not just recopying and pasting the output from the thread here which has already lost formatting.

For instance, it should look like

$ dpkg -l *dolfin* *ffc* *ufl* *dijitso* *petsc* | cat
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                  Version                        Architecture Description
+++-=====================================-==============================-============-======================================================================
ii  dolfin-bin                            2019.2.0~git20230116.bd54183-3 all          Executable scripts for DOLFIN
ii  dolfin-doc                            2019.2.0~git20230116.bd54183-3 all          Documentation and demo programs for DOLFIN
ii  dolfinx-doc                           1:0.6.0-1exp1                  all          Documentation and demo programs for DOLFIN
ii  libdolfin-dev:amd64                   2019.2.0~git20230116.bd54183-3 amd64        Shared links and development files for DOLFIN
ii  libdolfin-dev-common                  2019.2.0~git20230116.bd54183-3 all          Common header files for DOLFIN
...

I see, it looks readable right now. Something is wrong with the recognition. Once I locate a package by hand it returns the output at the bottom.

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                            Version                        Architecture Description
+++-===============================-==============================-============-================================================================
un  libpetsc-complex-3.6.2-dev      <none>                         <none>       (no description available)
un  libpetsc-complex-3.6.3-dev      <none>                         <none>       (no description available)
un  libpetsc-complex3.15-dbg        <none>                         <none>       (no description available)
un  libpetsc-complex3.15-dev        <none>                         <none>       (no description available)
ii  libpetsc-real-dev               3.15.5+dfsg1-1build2           all          Virtual package depending on latest PETSc development package
un  libpetsc-real3.10               <none>                         <none>       (no description available)
ii  libpetsc-real3.15:amd64         3.15.5+dfsg1-1build2           amd64        Shared libraries for version 3.15 of PETSc
un  libpetsc-real3.15-dbg           <none>                         <none>       (no description available)
ii  libpetsc-real3.15-dev:amd64     3.15.5+dfsg1-1build2           amd64        Static libraries, shared links, header files for PETSc
un  libpetsc3.10-dev-common         <none>                         <none>       (no description available)
un  libpetsc3.10-dev-examples       <none>                         <none>       (no description available)
un  libpetsc3.15                    <none>                         <none>       (no description available)
ii  libpetsc3.15-dev-common         3.15.5+dfsg1-1build2           all          Common header and support dev files for PETSc
ii  libpetsc3.15-dev-examples       3.15.5+dfsg1-1build2           all          Static libraries, shared links, header files for PETSc
un  libpetsc3.15.5-dev              <none>                         <none>       (no description available)
un  libpetsc3.6                     <none>                         <none>       (no description available)
un  libpetsc3.6.2-dev               <none>                         <none>       (no description available)
un  libpetsc3.6.3-dev               <none>                         <none>       (no description available)
ii  petsc-dev                       3.15.5+dfsg1-1build2           all          Virtual package depending on latest PETSc development package
un  petsc3.15-doc                   <none>                         <none>       (no description available)
un  python-dijitso                  <none>                         <none>       (no description available)
un  python-dijitso-doc              <none>                         <none>       (no description available)
un  python-petsc                    <none>                         <none>       (no description available)
un  python-petsc4py                 <none>                         <none>       (no description available)
ii  python-petsc4py-doc             3.15.1-2build1                 all          Python bindings for PETSc libraries: documentation and examples
un  python-petsc4py-docs            <none>                         <none>       (no description available)
un  python-ufl                      <none>                         <none>       (no description available)
ii  python-ufl-doc                  2022.2.0-2~ppa1~jammy1         all          documentation and demos for UFL
ii  python3-dijitso                 2019.2.0~git20190418.c92dcb0-2 all          distributed just-in-time building of shared libraries (Python 3)
ii  python3-petsc4py                3.15.1-2build1                 all          Python 3 bindings for PETSc libraries
un  python3-petsc4py-64-complex     <none>                         <none>       (no description available)
un  python3-petsc4py-64-complex3.15 <none>                         <none>       (no description available)
un dpkg-query: no packages found matching dolfin
dpkg-query: no packages found matching dolfinx-0.5.2
dpkg-query: no packages found matching ffc
 python3-petsc4py-64-real        <none>                         <none>       (no description available)
un  python3-petsc4py-64-real3.15    <none>                         <none>       (no description available)
un  python3-petsc4py-complex        <none>                         <none>       (no description available)
un  python3-petsc4py-complex3.15    <none>                         <none>       (no description available)
ii  python3-petsc4py-real           3.15.1-2build1                 all          Python 3 bindings for PETSc libraries (real numbers)
ii  python3-petsc4py-real3.15       3.15.1-2build1                 amd64        Python 3 bindings for PETSc 3.15 libraries (real numbers)
ii  python3-ufl                     2022.2.0-2~ppa1~jammy1         all          unified language for form-compilers (Python 3)

This is what the return looks like on advance of the following commands.

$ locate dolfin-bin
/usr/share/doc/dolfin-bin
/usr/share/doc/dolfin-bin/changelog.Debian.gz
/usr/share/doc/dolfin-bin/copyright
/var/cache/apt/archives/dolfin-bin_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_all.deb
/var/lib/dpkg/info/dolfin-bin.list
/var/lib/dpkg/info/dolfin-bin.md5sums

All of them seem to be located in /var/cache/apt/archives
Once I try to locate the same files, this is the actual return

$ locate 2019.2.0~git2023

/var/cache/apt/archives/dolfin-bin_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_all.deb
/var/cache/apt/archives/dolfin-doc_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_all.deb
/var/cache/apt/archives/libdolfin-dev-common_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_all.deb
/var/cache/apt/archives/libdolfin-dev_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_amd64.deb
/var/cache/apt/archives/libdolfin2019.2_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_amd64.deb
/var/cache/apt/archives/python3-dolfin-real_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_amd64.deb
/var/cache/apt/archives/python3-dolfin_2019.2.0~git20230116.bd54183-2~ppa1~jammy1_amd64.deb

Ok, there are a handful of systemic problems on your system. You’ve got inconsistent python versions (apparently you did that deliberately using update-alternatives). You’ve got a bizarre PETSc installation in /u/sw/toolchains/gcc-glibc/ I have no idea what that path means or where it comes from, except that it’s almost certainly going to interfere with getting dolfin running from ubuntu packages.

Your ubuntu is 22.04 jammy, which should itself be stable. It provides

python 3.10
dolfin 2019.2.0~git20210928.3eacdb4-3build1
petsc 3.15.5+dfsg1-1build2

The debian (and ubuntu) package system is a self-consistent system designed to ensure package dependencies are met so that programs can run successfully. If you start introducing external versions of packages, you can’t guarantee that interdependency is met, and programs will start to fail. Which is what has happened.

In particular python packages (such as dolfin) that provide python extensions written in C get built for a specific version of python and will fail on a python version that they were not built for. dolfin has other dependencies, PETSc among them, and will fail if the library versions it needs are not available.

Because of the strong dependency of the dolfin build on the python and petsc version, the dependency chain is managed by installing the dolfin python components in the tree of the PETSc installation, that is under the PETSC_DIR that dolfin is built for. dolfin.pth in the standard python installation path uses PETSC_DIR as a hint to find the dolfin module. So if you have PETSC_DIR pointing at some other random petsc installation (such as /u/sw/toolchains/gcc-glibc/) for which dolfin was not built, python won’t know where the dolfin module is.

Your creation of /bin/python3 is a problem. Why did you do that? python3 is normally located at /usr/bin/python3. You have two versions of python, 3.9 and 3.10. But Ubuntu 22.04 jammy provides python 3.10. Where does your python3.9 come from? (what does which python3 return?) Are you certain you completed the installation? (sudo apt update; sudo apt dist-upgrade. Keep repeating as many times as needed until it says there are no packages to update)

Having PETSC_DIR=/u/sw/toolchains/gcc-glibc/11.2.0/pkgs/petsc/3.15.1 is a problem, as discussed above. The dolfin package for ubuntu is not built against such a PETSc installation.

Your package list says dolfin and ffc not installed but this is probably interference from your local directory. Use quote marks in the command, or run dpkg -l in an empty directory. Check your python installation at the same time:

$ dpkg -l python3 python3-minimal python3-dev libpython3-dev "python3.*-minimal" "*dolfin*" "*ffc*" "*ufl*" "*dijitso*" "*petsc*" | cat