Skip to main content

Python language bindings for the preCICE coupling library

Project description

Python language bindings for the C++ library preCICE

Build status

This package provides python language bindings for the C++ library preCICE. Note that the first three digits of the version number of the bindings indicate the preCICE version that the bindings support. The last digit represents the version of the bindings. Example: v2.0.0.1 of the bindings represents version 1 of the bindings which is compatible with preCICE v2.0.0.

Required dependencies

  • preCICE: Refer to (the preCICE wiki)[] for information on installation.

Installing the package

We recommend using pip3 for the sake of simplicity.

Using pip3

preCICE system installs

For system installs of preCICE, this works out of the box.

You can either install from PyPI

$ pip3 install --user pyprecice

provide the link to this repository to pip (replace <branch> with the branch you want to use, preferably master or develop)

$ pip3 install --user<branch>.zip

or, if you cloned this repository, execute the following command from this directory:

$ pip3 install --user .

note the dot at the end of the line

This will fetch cython, compile the bindings and finally install the package pyprecice.

preCICE at custom location (setting PATHS)

If preCICE (the C++ library) was installed in a custom prefix, or not installed at all, you have to extend the following environment variables:

  • LIBRARY_PATH, LD_LIBRARY_PATH to the library location, or $prefix/lib
  • CPATH either to the src directory or the $prefix/include


preCICE system installs

In this directory, execute:

$ python3 install --user

preCICE at custom location (setting PATHS)

see above. Then run

$ python3 install --user

preCICE at custom location (explicit include path, library path)

  1. Install cython via pip3
$ pip3 install --user cython
  1. Open terminal in this folder.
  2. Build the bindings
$ python3 build_ext --include-dirs=$PRECICE_ROOT/src --library-dirs=$PRECICE_ROOT/build/last


  • --include-dirs=, default: '' Path to the headers of preCICE, point to the sources $PRECICE_ROOT/src, or the your custom install prefix $prefix/include.


  • If you build preCICE using CMake, you can pass the path to the CMake binary directory using --library-dirs.
  • It is recommended to use preCICE as a shared library here.
  1. Install the bindings
$ python3 install --user
  1. Clean-up optional
$ python3 clean --all

Test the installation

Update LD_LIBRARY_PATH such that python can find


Run the following to test the installation:

$ python3 -c "import precice"

Unit tests

  1. Clean-up mandatory (because we must not link against the real, but we use a mocked version)
$ python3 clean --all
  1. Set CPLUS_INCLUDE_PATH (we cannot use build_ext and the --include-dirs option here)
  1. Run tests with
$ python3 test


Troubleshooting & miscellaneous

preCICE is not found

The following error shows up during installation, if preCICE is not found:

  /tmp/pip-install-d_fjyo1h/pyprecice/precice.cpp:643:10: fatal error: precice/SolverInterface.hpp: No such file or directory
    643 | #include "precice/SolverInterface.hpp"
        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
  ERROR: Failed building wheel for pyprecice
Failed to build pyprecice
ERROR: Could not build wheels for pyprecice which use PEP 517 and cannot be installed directly

There are two possible reasons, why preCICE is not found:

  1. preCICE is not installed. Please download and install the C++ library preCICE. See above.
  2. preCICE is installed, but cannot be found. Please make sure that preCICE can be found during the installation process. See our wiki page on linking to preCICE and the instructions above.

Version of Cython is too old

In case the compilation fails with shared_ptr.pxd not found messages, check if you use the latest version of Cython.

Version of pip3 is too old

If you see the following error

error: option --single-version-externally-managed not recognized

your version of pip might be too old. Please update pip and try again. One possible way for updating pip is to run the following commands:

wget -q -O && python3

Be aware that python3 might require root privileges.

Check your version of pip via pip3 --version. For version 8.1.1 and 9.0.1 we know that this problem occurs. Remark: you get versions 8.1.1 of pip if you use sudo apt install python3-pip on Ubuntu 16.04 (pip version 9.0.1 on Ubuntu 18.04)

I'm using preCICE < 2.0.0, but there is no matching version of the bindings. What can I do?

If you want to use the old experimental python bindings (released with preCICE version < 2.0.0), please refer to the documentation of the corresponding preCICE version.

Installing the python bindings for Python 2.7.17

Note that the instructions in this section are outdated and refer to the deprecated python bindings. Until we have updated information on the installation procedure for the python bindings under this use-case, we will keep these instructions, since they might still be very useful (Originally contributed by huangq1234553 to the precice wiki in precice/precice/wiki:8bb74b7.)

show details

This guide provides steps to install python bindings for precice-1.6.1 for a conda environment Python 2.7.17 on the CoolMUC. Note that preCICE no longer supports Python 2 after v1.4.0. Hence, some modifications to the python setup code was necessary. Most steps are similar if not identical to the basic guide without petsc or python above. This guide assumes that the Eigen dependencies have already been installed.

Load the prerequisite libraries:

module load gcc/7
module unload
module load
module load cmake/3.12.1

At the time of this writing module load boost/1.68.0 is no longer available. Instead boost 1.65.1 was installed per the boost and yaml-cpp guide above.

In order to have the right python dependencies, a packaged conda environment was transferred to SuperMUC. The following dependencies were installed:

  • numpy
  • mpi4py

With the python environment active, we have to feed the right python file directories to the cmake command. Note that -DPYTHON_LIBRARY expects a python shared library. You can likely modify the version to fit what is required.

mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON -DPRECICE_PETScMapping=OFF -DPRECICE_PythonActions=ON -DCMAKE_INSTALL_PREFIX=/path/to/precice -DCMAKE_BUILD_TYPE=Debug .. -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  -DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR')+'/')") -DNumPy_INCLUDE_DIR=$(python -c "import numpy; print(numpy.get_include())")
make -j 12
make install

After installing, make sure you add the preCICE installation paths to your .bashrc, so that other programs can find it:

export PRECICE_ROOT="path/to/precice_install"
export PKG_CONFIG_PATH="path/to/precice_install/lib/pkgconfig:${PKG_CONFIG_PATH}"
export CPLUS_INCLUDE_PATH="path/to/precice_install/include:${CPLUS_INCLUDE_PATH}"
export LD_LIBRARY_PATH="path/to/precice_install/lib:${LD_LIBRARY_PATH}"

Then, navigate to the python_future bindings script.

cd /path/to/precice/src/precice/bindings/python_future

Append the following to the head of the file to allow Python2 to run Python3 code. Note that importing unicode_literals from future will cause errors in setuptools methods as string literals in code are interpreted as unicode with this import.

from __future__ import (absolute_import, division,
from builtins import (
         bytes, dict, int, list, object, range, str,
         ascii, chr, hex, input, next, oct, open,
         pow, round, super,
         filter, map, zip)

Modify mpicompiler_default = "mpic++" to mpicompiler_default = "mpicxx" in line 100. Run the setup file using the default Python 2.7.17.

python install --user


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyprecice- (23.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page