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.

Hi guys,

I have the same problem… and a already tried all this tips that you talking about and don’t work for me until now… /=

I try to run the simp_topology_optimization.ipynb:

%matplotlib notebook
import dolfin
import ufl
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np

And i have this error:

ImportError Traceback (most recent call last)
in
1 get_ipython().run_line_magic(‘matplotlib’, ‘notebook’)
----> 2 import dolfin
3 import ufl
4 from dolfin import *
5 import matplotlib.pyplot as plt

/usr/lib/petsc/lib/python3/dist-packages/dolfin/init.py in
142 from .common.plotting import plot
143
→ 144 from .fem.assembling import (assemble, assemble_system, assemble_multimesh, assemble_mixed,
145 SystemAssembler, assemble_local)
146 from .fem.form import Form

/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/assembling.py in
36 from dolfin.function.multimeshfunction import MultiMeshFunction
37
—> 38 from ufl.form import sub_forms_by_domain
39
40 all = [“assemble”, “assemble_mixed”, “assemble_local”, “assemble_system”,

ImportError: cannot import name ‘sub_forms_by_domain’ from ‘ufl.form’ (/home/cassiano/.local/lib/python3.8/site-packages/ufl/form.py)

But when I go check it in the form.py, the function sub_forms_by_domain don exists… =/

I searched on forum and a find some issues from the subdomain topic. Is this related?

I hope that someone could help me with this issue, please.

Cassiano Tavares
Best Regards

You haven’t tried all the tips, you still have a local copy of ufl:

You haven’t installed fenics from the Ubuntu or PPA repositories. Installation instructions for Ubuntu are at https://fenicsproject.org/download/ .

See also Ubuntu 18.04 - ImportError: cannot import name 'sub_forms_by_domain' - #7 by LiborKudela

Note also that Ubuntu 18.04 provides python 3.6, but your local file corresponds to python 3.8. You don’t have a standard Ubuntu 18.04 installation.

1 Like