# Ubuntu 18.04 - ImportError: cannot import name 'sub\_forms\_by\_domain'

**URL:** https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257
**Category:** installation
**Created:** [October 7, 2020, 11:57am UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257 "2020-10-07T11:57:18Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![LiborKudela](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/liborkudela/32/5444_2.png) [@LiborKudela](https://fenicsproject.discourse.group/u/LiborKudela)
#### Post date: [October 7, 2020, 11:57am UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/1 "2020-10-07T11:57:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![LiborKudela](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/liborkudela/32/5444_2.png) [@LiborKudela](https://fenicsproject.discourse.group/u/LiborKudela)
#### Post date: [October 7, 2020, 12:33pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/2 "2020-10-07T12:33:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)
#### Post date: [October 22, 2020, 7:06pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/4 "2020-10-22T19:06:26Z")

</div>

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'](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/2), I finally decided to use the official FEniCS docker container [`quay.io/fenicsproject/stable:current`](https://fenicsproject.org/download/), which works.

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

```auto
# 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 *"

```

---

<div class="post-metadata">

### Author: ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)
#### Post date: [October 22, 2020, 9:01pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/5 "2020-10-22T21:01:38Z")

</div>

Maybe @dparsons have some insight about this?🙂

---

<div class="post-metadata">

### Author: ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)
#### Post date: [October 23, 2020, 2:13am UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/6 "2020-10-23T02:13:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![LiborKudela](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/liborkudela/32/5444_2.png) [@LiborKudela](https://fenicsproject.discourse.group/u/LiborKudela)
#### Post date: [October 23, 2020, 1:06pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/7 "2020-10-23T13:06:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)
#### Post date: [October 23, 2020, 2:12pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/8 "2020-10-23T14:12:56Z")

</div>

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:

```auto
:~$ 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

```auto
echo $PYTHONPATH

```

---

<div class="post-metadata">

### Author: ![Lalson\_Vincent](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lalson_vincent/32/1057_2.png) [@Lalson\_Vincent](https://fenicsproject.discourse.group/u/Lalson_Vincent)
#### Post date: [December 10, 2020, 2:25pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/9 "2020-12-10T14:25:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![BenjaminRueth](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/benjaminrueth/32/397_2.png) [@BenjaminRueth](https://fenicsproject.discourse.group/u/BenjaminRueth)
#### Post date: [January 26, 2021, 11:53am UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/10 "2021-01-26T11:53:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Cassiano\_Tavares](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/cassiano_tavares/32/3059_2.png) [@Cassiano\_Tavares](https://fenicsproject.discourse.group/u/Cassiano_Tavares)
#### Post date: [October 10, 2021, 2:55pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/11 "2021-10-10T14:55:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)
#### Post date: [October 10, 2021, 10:31pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/12 "2021-10-10T22:31:18Z")

</div>

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

> [@Cassiano\_Tavares](#):
>
> /home/cassiano/.local/lib/python3.8/site-packages/ufl/form.py

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

See also [Ubuntu 18.04 - ImportError: cannot import name 'sub\_forms\_by\_domain' - #7 by LiborKudela](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/7)

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.

---

<div class="post-metadata">

### Author: ![egomezp](https://avatars.discourse-cdn.com/v4/letter/e/d78d45/32.png) [@egomezp](https://fenicsproject.discourse.group/u/egomezp)
#### Post date: [January 9, 2024, 4:52pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/13 "2024-01-09T16:52:29Z")

</div>

Hey there, I’m facing the same problem described on this topic. Code worked yesterday but the problem arose today. When executing the simulation:

```auto
ImportError: cannot import name 'sub_forms_by_domain' from 'ufl.form' (/home/edu/.local/lib/python3.8/site-packages/ufl/form.py)

```

I did some pip installing today, in order to use GMSH. I tried LiborKudela’s solution, removing the ufl package manually, by using

```auto
cd /home/edu/.local/lib/python3.6/site-packages/

sudo rm -r ufl/

```

When executing the first command, answer ‘No such file or directory’. I’m a bit lost.

I used CMD in order to get the folder where Python is installed. Path is as shown:

C:\Users\tooor\AppData\Local\Microsoft\WindowsApps\python.exe

Thanks in advance.

---

<div class="post-metadata">

### Author: ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)
#### Post date: [January 9, 2024, 5:40pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/14 "2024-01-09T17:40:26Z")

</div>

You have removed `ufl` from the `python3.6` local installation, but from the error you see that it is actually getting imported from the `python3.8` directory. Remove that one too.

---

<div class="post-metadata">

### Author: ![egomezp](https://avatars.discourse-cdn.com/v4/letter/e/d78d45/32.png) [@egomezp](https://fenicsproject.discourse.group/u/egomezp)
#### Post date: [January 10, 2024, 12:08pm UTC](https://fenicsproject.discourse.group/t/ubuntu-18-04-importerror-cannot-import-name-sub-forms-by-domain/4257/15 "2024-01-10T12:08:45Z")

</div>

Yes, I realised a bit later. Thanks anyways.

For anyone who needs it, I removed both ufl packages and installed again. Worked fine.
