How to import fenics to python3 jupyter notebook?

I am using debian stretch. I installed fenics from debian stretch’s apt repository, so the installed version of fenics is 1:2016.2.0.1. Yes a bit outdated, but I will just run a simple Poisson equation to play with fenics.

I open a jupyter notebook (which is using Python3) and as the first command I type:

from fenics import *

But I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-23499d756b21> in <module>
----> 1 from fenics import *

ImportError: No module named 'fenics'

On the terminal, the following command does not return any errors:

python2 -c 'import fenics'

So, I believe fenics is properly installed on my system. And the only problem seems to import it into python3.

Therefore the question is this: How can I import the fenics, as the version I have on my operating system, into a python3 jupyter notebook?

No ideas, anyone? I don’t think this is a complicated problem.

You need to create a port-forwarding fenics docker container. Instructions are in the fenics docs, here.

1 Like

Thanks for the tip.
However, I do not understand why I should install docker. Can’t I use fenics without docker? I am averse of installing more-than-necessary packages to my OS.

Maybe you can try the official PPA? https://fenics.readthedocs.io/en/latest/installation.html#debian-ubuntu-packages

I installed it earlier on Ubuntu 19.04 and it works just fine.

Yeah, I can do that.
The reason I didn’t do it to begin with was that I didn’t want to install packages from outside debian repos.
It seems like there is no way to use fenics package as is given in the official debian stretch repos…

So, here is what I think: The fenics package available on debian stretch repo uses python2 https://packages.debian.org/stable/math/fenics

Here it says “dep: python-instant; simple inlining of C/C++ code in Python 2”

So, the fenics version (1:2016.2.0.1) cannot be imported to python3, because that version of fenics uses python2.

On a second thought, I checked the fenics package that’s available in ubuntu 18.04 repo https://packages.ubuntu.com/bionic/math/fenics

The version of fenics there is (1:2017.2.0.1) and on its description page it still says “dep: python-instant; simple inlining of C/C++ code in Python 2”.

So, does that mean one cannot import the fenics as is given in the ubuntu repo (without PPA) to a python3 environment, too?

====================

All my pain is this: I just want to be able use the fenics package in a python3 environment as given in official repo of debian. Without docker. Without PPA.

Is it possible or not?

It is definitely possible!

All FEniCS versions >=2017.1.0 can be imported to Python3 according to documentation (See ChangeLog). Personally though I have only installed and checked versions >=2018.1.0 from the debian repo (although since I’m on Ubuntu, I could just use apt install and it worked just fine).

Just FYI, in 2017.1.0 (the version you were trying) parts of it were still to be ported to Python3 - The instant wrapper was one such older library which was decided to be deprecated and hence wasn’t ported. I’d suggest you install the Python2 dependencies and check, and if it still doesn’t work, try a newer version.

HARDER yet definitely possible solution : Build from source.

1 Like

Hello, did you solve this problem? I am having this same problem. I am trying to use fenics on Ubuntu 18.04, python 3.7.
Fenics is properly installed, still it cannot import module.
Please post the solution if you have solved this.

I am running FEniCS on Ubuntu 18.04 installed from the official repositories with Python 3.6.8 inside a Jupyter Notebook. Maybe you try to import fenics and not dolfin?