DOLFINx v0.9.0: `dolfinx.common.has_petsc4py` Returns `False` Despite PETSc and petsc4py Working

Background

I am installing DOLFINx v0.9.0 from source on Ubuntu 24.04 LTS with the goal of enabling MPI parallel support. I have successfully installed all dependencies, including PETSc 3.21.5 and petsc4py 3.21.5. However, when importing DOLFINx, I encounter an AssertionError because dolfinx.common.has_petsc4py returns False. I have tried multiple approaches, including recompiling the C++ core and Python interface, but the issue persists. I would appreciate the community’s help in diagnosing and resolving this problem.

System Environment

  • Operating System: Ubuntu 24.04 LTS
  • Python Version: 3.12.3 (in a virtual environment named fenicsx-env)
  • DOLFINx Version: v0.9.0 (built from GitHub source)
  • PETSc Version: 3.21.5 (built from source, installed at /usr/local/petsc)
  • petsc4py Version: 3.21.5 (installed via pip)
  • MPI Implementation: MPICH 4.1 (installed via sudo apt install mpich)
  • HDF5: System version (/usr/lib/x86_64-linux-gnu/hdf5/mpich, installed via sudo apt install libhdf5-mpich-dev, supports parallel)
  • CMake Version: 3.29.6 (built from source)

Installation Steps

  1. Installed PETSc:
    git clone -b v3.21.5 PETSc / petsc · GitLab petsc
    cd petsc
    ./configure --with-mpi=1 --with-hdf5=1 --with-hdf5-dir=/usr/lib/x86_64-linux-gnu/hdf5/mpich --prefix=/usr/local/petsc
    make PETSC_DIR=(pwd) PETSC_ARCH=arch-linux-c-opt all -j(nproc)
    sudo make PETSC_DIR=$(pwd) PETSC_ARCH=arch-linux-c-opt install
    export PETSC_DIR=/usr/local/petsc
    export PETSC_ARCH=arch-linux-c-opt

  2. Installed petsc4py:
    source ~/fenicsx-env/bin/activate
    PETSC_DIR=/usr/local/petsc PETSC_ARCH=arch-linux-c-opt pip install petsc4py==3.21.5

  3. Installed DOLFINx C++ Core:
    cd ~/Downloads/dolfinx/cpp/build
    cmake -DCMAKE_BUILD_TYPE=Release

-DCMAKE_PREFIX_PATH=“/usr/local;/usr/local/petsc;/usr/lib/x86_64-linux-gnu/hdf5/mpich”

-DPython3_EXECUTABLE=$(which python3)

-DHDF5_ROOT=/usr/lib/x86_64-linux-gnu/hdf5/mpich

-DPETSC_DIR=/usr/local/petsc

-DPETSC_ARCH=arch-linux-c-opt

..
make -j$(nproc)
sudo make install

  1. Installed DOLFINx Python Interface:
    cd ~/Downloads/dolfinx/python
    PETSC_DIR=/usr/local/petsc PETSC_ARCH=arch-linux-c-opt pip install .

  2. Environment Variables:
    export LD_LIBRARY_PATH=/usr/local/petsc/lib:/usr/lib/x86_64-linux-gnu/hdf5/mpich:$LD_LIBRARY_PATH

Issue Description

When importing DOLFINx, I get the following error:
python -c “import dolfinx; print(dolfinx.version)”
Traceback (most recent call last):
File “”, line 1, in
File “/home/asus/fenicsx-env/lib/python3.12/site-packages/dolfinx/init.py”, line 20, in
assert dolfinx.common.has_petsc4py
^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

The same error occurs with parallel execution:
mpirun -np 4 python -c “import dolfinx; print(dolfinx.version)”

Debugging Information

  1. petsc4py Verification:
    python -c “import petsc4py; print(petsc4py.version)”
    Output:
    3.21.5

python -c “import petsc4py; petsc4py.init(); from petsc4py import PETSc; print(PETSc.Sys.getVersion())”
Output:
(3, 21, 5)

  • petsc4py can initialize PETSc correctly.
  1. Debugging dolfinx/__init__.py:
    Modified to include:

import petsc4py
print(“petsc4py version:”, petsc4py.version)
from dolfinx import cpp
print(“cpp module loaded:”, cpp)
print(“has_petsc4py before assert:”, dolfinx.common.has_petsc4py)
assert dolfinx.common.has_petsc4py, “petsc4py not detected”
Output:
petsc4py version: 3.21.5
cpp module loaded: <module ‘dolfinx.cpp’ from ‘/home/asus/fenicsx-env/lib/python3.12/site-packages/dolfinx/cpp.cpython-312-x86_64-linux-gnu.so’>
has_petsc4py before assert: False
Traceback (most recent call last):

AssertionError: petsc4py not detected
The dolfinx.cpp module loads successfully, but has_petsc4py is False.

  1. Debugging dolfinx/common.py:
    Modified to include:
    from dolfinx import cpp as _cpp
    import petsc4py
    petsc4py.init()
    from petsc4py import PETSc
    print(“PETSc version from petsc4py:”, PETSc.Sys.getVersion())
    print(“has_petsc4py from cpp:”, _cpp.common.has_petsc4py())
    Output:
    PETSc version from petsc4py: (3, 21, 5)
    has_petsc4py from cpp: False
  2. CMake Output (Partial):
    – Found PETSc version 3.21.5, prefix: /usr/local/petsc
    – Found MPI_C: /usr/lib/x86_64-linux-gnu/mpich/lib/libmpich.so (found suitable version “4.1”, minimum required is “3”)
    – Found HDF5: /usr/lib/x86_64-linux-gnu/hdf5/mpich/libhdf5.so …
  3. Library Files:

ls /usr/local/petsc/lib | grep libpetsc
Output:
libpetsc.so libpetsc.so.3.21 libpetsc.so.3.21.5

ls /usr/local/lib | grep libdolfinx
Output:
libdolfinx.so libdolfinx.so.0.9 libdolfinx.so.0.9.0

Problem Analysis

Both petsc4py and PETSc work correctly, and the dolfinx.cpp module loads without issues.
However, dolfinx.common.has_petsc4py (exposed via dolfinx.cpp) always returns False, indicating that the DOLFINx C++ core does not detect or enable PETSc.
The CMake output confirms that PETSc is found, but it’s unclear if it is fully enabled (e.g., whether the HAS_PETSC macro is defined).

Steps Tried

Verified PETSC_DIR and PETSC_ARCH environment variables.
Added /usr/local/petsc/lib to LD_LIBRARY_PATH.
Recompiled the DOLFINx C++ core and Python interface multiple times.
Added -DENABLE_PETSC=ON to CMake, but no change.
Debugged dolfinx/init.py and dolfinx/common.py to confirm petsc4py and dolfinx.cpp behavior.

Request for Help

Can someone clarify how DOLFINx v0.9.0’s C++ core detects and enables PETSc during the build process?
What could cause dolfinx.common.has_petsc4py to return False even though PETSc and petsc4py are functional?
Are there specific CMake options or patches needed to ensure PETSc is fully enabled?
If this is a compatibility issue, which PETSc version is recommended for DOLFINx v0.9.0?

Thank you for your assistance!

I guess your PETSc installation has some problem, which causes it is not successfully included in FEniCSx’ installation. You may want to use the one or several of the following:

PETSC_ARCH=linux-gnu-real32-32:linux-gnu-complex64-32:linux-gnu-real64-32:linux-gnu-complex128-32:linux-gnu-real64-64:linux-gnu-complex128-64

See the recommended installation by the FEniCS dev team: dolfinx/docker/Dockerfile.test-env at d04eba8996a51a5e0c2d02691577da81302ab0fe · FEniCS/dolfinx · GitHub
and also the way to install FEniCSx: Build 64bit petsc and dolfinx v0.8.0 - #3 by dokken