Installing FEniCS Python package inside Slicer

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.

Slicer FEniCS extension should help: Slicer FEniCS extension

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

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

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.

Hi @lassoan, that is the extension that I developed :slight_smile: 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:

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:

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

This is all the other files I could find:

$ 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:

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.

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

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

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)

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

     {MY_SLICER_DIR}/bin/PythonSlicer -m pip install fenics
    
  3. Download and install Python header files in Slicer:

     wget -P ${MY_SLICER_DIR}/lib/Python/include/python3.6m/ https://github.com/python/cpython/archive/v3.6.7.tar.gz
     cd ${MY_SLICER_DIR}/lib/Python/include/python3.6m/
     tar --strip-components=2 -zxf v3.6.7.tar.gz cpython-3.6.7/Include
    
  4. Install Python interface of DOLFIN:

     ${MY_SLICER_DIR}/bin/PythonSlicer -m pip install ${MY_FENICS_SRC}/dolfin/python
    

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.

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

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

2 Likes