# Installing FEniCS Python package inside Slicer

**URL:** <https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322>\
**Category:** Support\
**Created:** [July 1, 2020, 2:13pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322 "2020-07-01T14:13:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 1, 2020, 2:13pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/1 "2020-07-01T14:13:12Z")

</div>

Hi,

I need some help with installing the `fenics` Python package inside Slicer. I tried using `pip_install('fenics')` in the latest Slicer preview (4.11.0-2020-06-30 r29196 / 07bc921):

```
Python 3.6.7 (default, Jul 1 2020, 03:07:22) 
[GCC 5.3.1 20160406 (Red Hat 5.3.1-6)] on linux2
>>> pip_install('fenics')
Collecting fenics
  Using cached fenics-2019.1.0-py3-none-any.whl (1.5 kB)
Collecting fenics-ffc<2019.2,>=2019.1.0
  Using cached fenics_ffc-2019.1.0.post0-py3-none-any.whl (362 kB)
Collecting fenics-dijitso<2019.2,>=2019.1.0
  Using cached fenics_dijitso-2019.1.0-py3-none-any.whl (46 kB)
Collecting fenics-ufl<2019.2,>=2019.1.0
  Using cached fenics_ufl-2019.1.0-py3-none-any.whl (282 kB)
Collecting fenics-fiat<2019.2,>=2019.1.0
  Using cached fenics_fiat-2019.1.0-py3-none-any.whl (112 kB)
Requirement already satisfied: numpy in ./Slicer-4.11.0-2020-06-30-linux-amd64/lib/Python/lib/python3.6/site-packages (from fenics-ffc<2019.2,>=2019.1.0->fenics) (1.18.1)
Collecting sympy
  Using cached sympy-1.6-py3-none-any.whl (5.8 MB)
Processing /home/ben/.cache/pip/wheels/63/9d/8e/37c3f6506ed3f152733a699e92d8e0c9f5e5f01dea262f80ad/mpmath-1.1.0-cp36-none-any.whl
Installing collected packages: fenics-dijitso, fenics-ufl, mpmath, sympy, fenics-fiat, fenics-ffc, fenics
  WARNING: The script dijitso is installed in '/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64/lib/Python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script isympy is installed in '/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64/lib/Python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts ffc and ffc-3 are installed in '/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64/lib/Python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed fenics-2019.1.0 fenics-dijitso-2019.1.0 fenics-ffc-2019.1.0.post0 fenics-fiat-2019.1.0 fenics-ufl-2019.1.0 mpmath-1.1.0 sympy-1.6
WARNING: You are using pip version 20.1; however, version 20.1.1 is available.
You should consider upgrading via the '/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64/bin/./python-real -m pip install --upgrade pip' command.
>>> import dolfin
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'dolfin'
>>> import fenics
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'fenics'
>>> import ffc
>>> import dijitso

```

It appears that the installation is successful, but I cannot import the `fenics` or `dolfin` modules (although the related `ffc` and `dijitso` modules can be loaded).

I am using Ubuntu 18.04 and have FEniCS installed using PPA. In other words the non-Python components are available on my system path.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 1, 2020, 2:49pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/2 "2020-07-01T14:49:46Z")

</div>

Slicer FEniCS extension should help: [Slicer FEniCS extension](https://discourse.slicer.org/t/slicer-fenics-extension/7407)

Let us know if you find it useful, we would then consider making it available directly in the Extension manager.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 1, 2020, 2:53pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/3 "2020-07-01T14:53:56Z")

</div>

I’ve just realized that you are the person who develops Slicer FEniCS extension…

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 1, 2020, 3:00pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/4 "2020-07-01T15:00:45Z")

</div>

If you have a `fenics` folder in your `lib/Python/Lib` folder and it has a ` __init__.py` file then the module should be found. You can edit the init file or other Python files to add logs to figure out where things break down.

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 1, 2020, 3:01pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/5 "2020-07-01T15:01:11Z")

</div>

Hi @lassoan, that is the extension that I developed 🙂 I am trying to extend it for a different problem and also to simplify the installation. I have lost my previous development environment and it was quite difficult to install back then. I was hoping there may be a better way to install the Python modules. Last time I did it like this:

> **[Slicer FEniCS Installation](https://projectweek.na-mic.org/PW31_2019_Boston/Projects/SlicerFEniCS/slicer-fenics-install.html)**
>
> Website for NA-MIC Project Weeks

However, I have now installed FEniCS binaries instead of from source. I would like to just install the Python modules inside Slicer so I can import `fenics` or `dolfin` inside my extension module.

Here is the `fenics` package on pypi:

> **[fenics](https://pypi.org/project/fenics/)**
>
> The FEniCS Project Python Metapackage

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 1, 2020, 3:11pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/6 "2020-07-01T15:11:43Z")

</div>

I had a look and this is what I have in `/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64/lib/Python/lib/python3.6/site-packages/`:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/0/90e6106756090ddedec2db5af96b51e24cba3b9e.png)

This is all the other files I could find:

```auto
$ pwd
/opt/slicer/Slicer-4.11.0-2020-06-30-linux-amd64
$ find . -iname fenics*
./lib/Python/lib/python3.6/site-packages/fenics_dijitso-2019.1.0.dist-info
./lib/Python/lib/python3.6/site-packages/fenics_ufl-2019.1.0.dist-info
./lib/Python/lib/python3.6/site-packages/fenics_fiat-2019.1.0.dist-info
./lib/Python/lib/python3.6/site-packages/fenics_ffc-2019.1.0.post0.dist-info
./lib/Python/lib/python3.6/site-packages/fenics-2019.1.0.dist-info
$ find . -iname dolfin*
./lib/Python/lib/python3.6/site-packages/ffc/backends/dolfin

```

This is where the system installed dolfin module is:

```auto
In [1]: import dolfin

In [2]: dolfin. __file__
Out[2]: '/usr/lib/python3/dist-packages/dolfin/ __init__.py'

In [3]: 

```

So the one in Slicer should have a similar name I guess but with a different path.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 1, 2020, 3:28pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/7 "2020-07-01T15:28:39Z")

</div>

This seems to be a common issue and troubleshooting tips are available here: [https://stackoverflow.com/questions/50585246/pip-install-creates-only-the-dist-info-not-the-package](https://stackoverflow.com/questions/50585246/pip-install-creates-only-the-dist-info-not-the-package)

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 1, 2020, 3:42pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/8 "2020-07-01T15:42:44Z")

</div>

Thanks, I will try that. What is the best/recommended way to run pip to install/uninstall packages in Slicer? I have looked on the forum, wiki, documentation and there appear to be a few different ways to run pip. Do you have a link to the most up to date documentation for this?  
Currently I am using this: [https://www.slicer.org/wiki/Documentation/Nightly/Developers/FAQ/Python\_Scripting#How\_to\_run\_pip\_.3F](https://www.slicer.org/wiki/Documentation/Nightly/Developers/FAQ/Python_Scripting#How_to_run_pip_.3F)

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 1, 2020, 4:19pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/9 "2020-07-01T16:19:31Z")

</div>

I tried troubleshooting. I realised that the DOLFIN Python component needs to be built separately to link with the DOLFIN C++ libraries.

I got it working now using a “hybrid” approach:

1. Install FEniCS 2019.1.0 using Ubuntu PPA  
(see: [https://fenics.readthedocs.io/en/latest/installation.html#ubuntu-ppa](https://fenics.readthedocs.io/en/latest/installation.html#ubuntu-ppa))

2. Install `fenics` Python-only components in Slicer using `pip`:

3. Download and install Python header files in Slicer:

4. Install Python interface of DOLFIN:

Step 3 was suggested by @jcfr when I was developing Slicer FEniCS in Boston. Maybe it would be worthwhile to include these headers within the Slicer distribution. It is the only complicated step in the installation process here.

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [July 9, 2020, 4:33pm UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/10 "2020-07-09T16:33:48Z")

</div>

> <https://github.com/Slicer/Slicer/issues/2637>
>
> This issue was created automatically from an original Mantis Issue. Further discussion may take place here.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 10, 2021, 8:42am UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/11 "2021-02-10T08:42:29Z")

</div>

Hi Andras,  
I have fenics in the 3d slicer. when I use interpreter I can import the fenics  
but how can i import from within a module it says

Traceback (most recent call last):  
File “”, line 1, in   
File “/home/saima/Downloads/Slicer-4.11.20200930-linux-amd64/lib/Python/lib/python3.6/imp.py”, line 170, in load\_source  
module = \_exec(spec, sys.modules[name])  
File “”, line 618, in \_exec  
File “”, line 674, in exec\_module  
File “”, line 781, in get\_code  
File “”, line 741, in source\_to\_code  
File “”, line 219, in \_call\_with\_frames\_removed  
File “/home/saima/Downloads/Slicer-4.11.20200930-linux-amd64/VoxelizeMesh/crwateMesh/crwateMesh.py”, line 317  
def process(self, inputVolume, outputVolume, imageThreshold, invert=False, showResult=True):  
^  
SyntaxError: import \* only allowed at module level

from fenics import \* is not allowed how can I fix it?

thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![benzwick](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/benzwick/32/80521_2.png) [@benzwick](https://discourse.slicer.org/u/benzwick)\
**Post date:** [February 10, 2021, 11:28am UTC](https://discourse.slicer.org/t/installing-fenics-python-package-inside-slicer/12322/12 "2021-02-10T11:28:47Z")

</div>

You can have a look in this file to see how to use FEniCS DOLFIN inside a 3D Slicer module:

> <https://github.com/benzwick/slicer-fenics/blob/master/FEniCS_demo/FEniCS_demo_module/FEniCS_demo_module.py>
