Ubuntu 18.04 - ImportError: cannot import name 'sub_forms_by_domain'

Hi,

I have updated my system today (Ubuntu 18.04) and fenics is no longer working.

MWE:

from fenics import *

This produces:

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 *
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", line 144, in <module>
    from .fem.assembling import (assemble, assemble_system, assemble_multimesh, assemble_mixed,
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/assembling.py", line 38, in <module>
    from ufl.form import sub_forms_by_domain
ImportError: cannot import name 'sub_forms_by_domain'

I tried to reinstall fenics using:

sudo apt-get autoremove fenics
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:fenics-packages/fenics
sudo apt-get update
sudo apt-get install fenics

But I still get the same error.
Any help would be appreciated. Thanks.

2 Likes

Let me answer my own question.

I have followed where does Python import the ufl package from and I have found another version of it located here:

/home/username/.local/lib/python3.6/site-packages

I have removed it and it seems to be working now.

4 Likes

Thanks for this post! I already thought that I’m totally alone with this problem.

I have been struggling with this issue also since about 2 weeks ago (Ubuntu 18.04 and 20.04). Since I was working in docker containers and I could not fix it with your solution suggested in Ubuntu 18.04 - ImportError: cannot import name 'sub_forms_by_domain', I finally decided to use the official FEniCS docker container quay.io/fenicsproject/stable:current, which works.

In case anybody wants to reproduce the issue, here is a Dockerfile:

# Using ubuntu 20.04 as basis
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

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 install -y --no-install-recommends fenics
RUN python3 -c "from fenics import *"
RUN apt-get install -y python3-pip
RUN pip3 install --user ufl
RUN python3 -c "from fenics import *"
1 Like

Maybe @dparsons have some insight about this?:slight_smile:

As LiborKudela indicated, the first place to look is whether you’ve got any user-installed python modules, installed manually by pip for instance. If you have that then your versions will not be consistent and you will get this kind of error. Python gives priority to user installed modules.

1 Like

When I was trying to remove the package using pip3 uninstall ufl I could not fix it either, since pip did not see it for some reason. I think that uninstalling the package using pip, leaves some stuff in the site-packages directory.

I had to remove the ufl package manually using:

cd /home/username/.local/lib/python3.6/site-packages/
sudo rm -r ufl/

I should have mentioned that earlier.

3 Likes

I’m late to the discussion, but you can also just see where python is picking up your package and remove it if necessary. Your PYTHONPATH be be prioritising directories containing old versions.

e.g. for me:

:~$ python3 -c "import ufl; print(ufl.__file__)"
$HOME/local/lib/python3.8/site-packages/fenics_ufl-2019.2.0.dev0-py3.8.egg/ufl/__init__.py

also worth checking your PYTHONPATH to see if there’s something there which shouldn’t be

echo $PYTHONPATH

I uninstalled everything and installed ubuntu again. Didnt face this problem the second time.

If possible, I would like to report this issue (me and my collaborators are frequently running into it on different systems, also without any previous history). I just don’t know where. Can anybody help me here? Sorry, if this is slightly off-topic, but from my point of view the solution above offers help to FEniCS users, but does not really solve the underlying problem.