FenicsX in PyCharm

Hi all, I am using Fenics with PyCharm. In order to make it work I configured a python remote interpreter with Quay (which is located in Docker).
Now I want to use FenicsX because it supports hexahedral meshes. I installed dolfinx/dolfinx and also dolfinx/dev-env (not sure what the difference is) in Docker. Then I configured the remote interpreter in PyCharm using dolfinx/dolfinx or dolfinx/dev-env. In both cases I get " ModuleNotFoundError: No module named ‘dolfinx’ " when I run the demo_poisson.py file (dolfinx version).
My impression is that PyCharm can only be configured with quay.io type of Docker images. If so, would it be possible to create such a Docker image? How?
Of course, there might be other solutions.
I don’t know how to continue, any advice is highly appreciated! Thanks.

1 Like

The dev-env image only has the dependencies of DOLFINx.

The image you should use is dolfinx/dolfinx.
I’m not sure what you imply by

The image dolfinx/dolfinx is hosted on Dockerhub which is the native place to have docker images.

The problem is that pycharm ignores the PYTHONPATHs that are set in the Dockerfile: python - PyCharm and PYTHONPATH - Stack Overflow
You can change this “Settings->Python Interpreter” then press the settings wheel right of “Python Interpreter”, then choose “Show all” and then “show path for selected interpreter” and add the path “/usr/local/dolfinx-real/lib/python3.10/dist-packages”. This makes the python console work.

I do not know how to fix this for the “Run” button, as it seems to ignore the python paths.

Thanks for the quick reply.
I checked and the path you mention is already there. Indeed the python console works (i.e. dolfinx is not underlined red in the statement “import dolfinx”).
The Run button does work for docker image “quay .io / fenicsproject / stable:latest”.
Hence my impression that an image called (for example) “quay.io/fenicsproject/dolfinx:latest” would likewise work for the Run button. This image would need to be created (in the same manner that the other one was created).
The interpreter paths for “Quay” are (not sure if this is useful):

The point is that this is an issue with pycharm choosing to ignore the python paths set in the the dolfinx/dolfinx environment (even when you configure the pycharm environment to have the python-path). I would suggest submitting a issue/bug to the pycharm developers. They might know a workaround for this (see: https://youtrack.jetbrains.com/issue/PY-23396/PYTHONPATH-is-replaced-by-PyCharm-when-running-in-SSH-interpreter).

The reason for DOLFINx having custom python paths is that the dolfinx/dolfinx docker image contains both a real and complex build of DOLFINx (matching the suitable petsc installation.
This means that one cannot install it directly into /usr/local/lib/python3.10/dist-packages (as there are two separate builds).

You can create your own docker image, that install DOLFINx in /usr/local/lib/python3.10, by some small modifications of the DOLFINx dockerfile, by changing the --target, and removing the builds you do not use.

1 Like

I’ll contribute my 2 cents in case this helps someone:

I use a slight adaptation of the docker image provided dolfinx/Dockerfile at main · FEniCS/dolfinx · GitHub (I added some packages here and there).

I use the following configurations. It sets the correct environment variables at startup of the console

These are the environment variables:

And these are the settings for Docker itself

  • Wouter
1 Like

Hi Wouter, thanks for the 2 cents! I added User environment variables like this:

Now I get a warning after “import dolfinx” (instead of an error):
WARNING:py.warnings:/usr/local/lib/python3.10/dist-packages/petsc4py/lib/init.py:128: UserWarning: ignored arch: ‘linux-gru-real-32’, using: ‘linux-gnu-real-32’

(I have a 64 bit computer, maybe that is the reason?)

Now the script starts and then I encounter the next error:
NameError: name ‘RectangleMesh’ is not defined

Probably the syntax in FenicsX changed.

Unfortunately I will need to add the User environment variables to each new script I create (there does not seem to be a general location where these User environment variables are stored).

Dokken, I tried creating my own docker image (after adapting the Dockerfile), but I got a lot of errors. I also tried installing FenicsX in anaconda but I got this error:

PackagesNotFoundError: The following packages are not available from current channels:

  • mpich
  • fenics-dolfinx

Anyway, next step for me is to figure out how to convert my Fenics scripts to the Fenicsx syntax.

1 Like

Is it a typo? gru <-> gnu

1 Like

fenics-doflinx is on conda-forge: Fenics Dolfinx :: Anaconda.org
and should be installed as shown in: GitHub - FEniCS/dolfinx: Next generation FEniCS problem solving environment

Wouter, yes indeed typo. Now the warning is gone.

Dokken, the command “conda install -c conda-forge fenics-dolfinx mpich pyvista” is indeed what I used.

I prefer to continue with PyCharm, otherwise I will have two different development environments.

Apparently creating simple meshes has now changed to:

For other geometries I need to use gmsh as explained here:
Generating a (Rectangle-Circle)-Mesh in dolfinx - mesh - FEniCS Project

Hello @wvanharten, I’m confronted with a smiliar problem like @michiel and even setting the environment variables didn’t help. Here is my setup:

Screenshot 2022-08-31 141358

Anything else I missed here?

Hi, I can have a better look later. But at a first glance, have you updated the PYTHONPATH environment variable? In my image it’s highlighted in blue which indicates it’s changed as well.

For me, /usr/local/dolfinx-complex/lib/python3.8/dist-packages has been prepended to PYTHONPATH, you may need a different python version though.

Basically, I adapted These lines from the DockerFile.

  • Wouter

Thank’s Wouter, that makes sense. I don’t have dolfinx-complex or dolfinx-real folders on my computer though. I only have a dolfinx-folder like this:



I’m pretty new to fenics, so please excuse these stupid questions, but I’m not quite sure where to get dolfinx-complex (or dolfinx-real). Do I have to install it manually? Actually, I thought everything is done in the docker image dolfinx/dolfinx:stable when used as a remote interpreter inside the PyCharm IDE?!

Hi Oskar,

These settings are all referring to paths inside the container.

This is exactly the mayor strength of dockerization, your particular local setup (What you do and don’t have installed on your pc) does not matter as all the code is executed in the container making use of software installed in the container.

The dolfinx-real and dolfinx-complex folders are folders inside the docker container.

Does this make it more clear?

  • Wouter

Yes, thank you. My problem was adding the new variable /usr/local/dolfinx-real/lib/python3.10/dist-packages:/usr/local/lib: in windows system variables so it would be listed in the lower half of the Environment Variables in PyCharm as shown in your picture.

Adding /usr/local/dolfinx-real/lib/python3.10/dist-packages:/usr/local/lib: as environment variable as shown by michiel did the trick for me.

Now the package dolfinx can be imported without an error. A new error has appeared concerning another missing package called pyvista, which I guess I can import by modifying the original Docker image.

Thank’s for the help!

Hello. I just came across this thread in setting up PyCharm with DOLFINx. The code that I ported over seems to run DOLFINx ok, but I have some warning/error messages about “unresolved references” and I do not know where they are coming from:
Screen Shot 2023-03-06 at 1.14.07 PM

Also, with the docker python interpreter, pyvista can’t be found even though I sucessfully use it in my previous workflow (just jupyter notebook/anaconda navigator).

For reference, I chose to use Docker for my python interpreter because PyCharm in Settings-> Project: ProjectName → Python interpreter cannot find my anaconda fenics environment. Interestingly, the Run/Debug configuration CAN find the anaconda fenics environment.

Did anyone else get the warnings/errors on the imports? Any advice on how to install packages with the docker python interpreter?

Thank you.