ImportError: cannot import name 'AbstractFiniteElement' from 'ufl.finiteelement'

After installing in WSL2, Ubuntu 22.04.3 I’m finding that when running demo_elasticity.py as cloned from github’s url for dolfinx package::::

prusso@Yoga730-15ikb:~/dolfinx/python$  cd /home/prusso/dolfinx/python ; /usr/bin/env /bin/python3 /home/prusso/.vscode-server/extensions/ms-python.python-2023.22.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 39953 -- /home/prusso/dolfinx/python/demo/demo_elasticity.py 
Traceback (most recent call last):
  File "/home/prusso/dolfinx/python/demo/demo_elasticity.py", line 31, in <module>
    import dolfinx
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/__init__.py", line 40, in <module>
    from dolfinx import fem, geometry, graph, io, jit, la, log, mesh, nls, plot
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/__init__.py", line 12, in <module>
    from dolfinx.fem.assemble import (apply_lifting, assemble_matrix,
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/assemble.py", line 22, in <module>
    from dolfinx.fem.forms import Form
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/forms.py", line 17, in <module>
    from dolfinx import default_scalar_type, jit
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/jit.py", line 17, in <module>
    import ffcx.codegeneration.jit
  File "/usr/lib/python3/dist-packages/ffcx/codegeneration/jit.py", line 21, in <module>
    import ffcx.naming
  File "/usr/lib/python3/dist-packages/ffcx/naming.py", line 16, in <module>
    from .element_interface import convert_element
  File "/usr/lib/python3/dist-packages/ffcx/element_interface.py", line 12, in <module>
    import basix.ufl
  File "/home/prusso/.local/lib/python3.10/site-packages/basix/ufl.py", line 15, in <module>
    from ufl.finiteelement import AbstractFiniteElement as _AbstractFiniteElement
ImportError: cannot import name 'AbstractFiniteElement' from 'ufl.finiteelement' (/usr/lib/python3/dist-packages/ufl/finiteelement/__init__.py)
prusso@Yoga730-15ikb:~/dolfinx/python$ 

Anyone know why this particular ImportError and/or how to make things resolve and start to run the script?

You can’t mix an installation of dolfinx from the PPA with an installation of basix from pip. Run pip uninstall fenics-basix and verify that you are prompted to remove /home/prusso/.local/lib/python3.10/site-packages/basix.

Furthermore don’t go and download the demos from GitHub, because they might be for a newer version than the one you have installed. When install from the PPA, you have a package

dolfinx-doc Documentation and demo programs for DOLFIN

that install demos in /usr/share/doc/dolfinx. Copy them from there to your home.

1 Like