Creating a Conda package from Fortran and Python

Hi folks,

I thought this blog post that I just wrote might be of interest to some of you: Creating a Conda package for a project with Fortran and Python code · Sam Harrison

It’s meant as a tutorial for creating a Conda package for some Python code that calls a Fortran library, but brushes across a couple of subjects I know have been discussed here. Namely, calling Fortran from Python (I opt for ctypes) and difficulties of building Conda packages for Windows due to lack of packaged compilers.

I’d be interested to hear if anyone has any better ways of doing anything than what I’ve suggested in the post. Conda packaging is pretty new to me, so I’m very much still learning!

7 Likes

Hi Sam,

thanks for sharing. In the past I’ve found Conda to be easiest way to obtain and work with Python packages.

Regarding better ways, I might only share this pypackaging website that appeared recently, and discusses some pitfalls in Python packaging. There was also a related discussion on HackerNews. Many of the difficulties in packaging Fortran are shared with C and C++ (although there are a few Fortran specific ones).

1 Like

Nice write-up, generally a quite solid guide. A few suggestions though

  • if you don’t need to be compatible with the conda-forge ABI then you can in principle use any Fortran compiler for building your library, e.g. ifort on Windows. The crucial step is the rpath fixing provided by conda-build, if you pass those to the compiler you should be fine.
  • regarding environment variables, best use $FC $FFLAGS instead of $GFORTRAN, the former is more portable especially with passing through the options relevant for conda-build.

Finally, I would like to raise the issue with the two-stage build, I did it myself for the first Python bindings I wrote and it caused me a lot of work ever since, it is convenient to get started but not long-term feasible in my opinion. I now migrated to meson-python as build backend to get a simple one-step solution for pip install, while having the convenience of a proper build system for building the Fortran library.

2 Likes

Thanks both, great suggestions and links! I’m going to dig into meson-python a bit more. I’ve used meson a little for pure Fortran projects and was impressed.