Packaging legacy Fortran in Python. Is it OK to use setuptools and numpy.distutils?

PythonNumpyFortranSetuptoolsF2py

Python Problem Overview


I am trying to make a python package distribution for some popular Fortran codes in my field. I want it to use the most standard approach with a setup.py file. The related qustion was helpful for learning how to wrap Fortran extensions.

When using this approach, I noticed some confusing behavior when mixing setuptools and numpy.distutils. Is it bad practice to mix the two? As of 2015, it seems preferable to use setuptools as much as possible.

However, I would like to build Fortran extensions in a way that is compatible with numpy. So I would like to import from numpy.distutils to get Extension and setup.

I'm using the following basic approach:

from setuptools.command.develop import develop
from numpy.distutils.core import Extension, setup

ext_modules=[Extension("my_package.fortran_mod", sources=['src/fortran_mod.f'])]

class MyDevelop(develop):
  def run(self):
    my_script()
    develop.run(self)

setup(
  ...
  ext_modules=ext_modules,
  cmdclass={'develop':MyDevelop})

This seems to work but I have questions.

  1. Is it generally good practice to mix setuptools and numpy.distribute?
  2. Does the order I import them matter? Should I always import setuptools first?
  3. Is there an official up-to-date tutorial for packaging extensions to numpy? Perhaps even one with some discussion Fortran extensions?


https://www.youtube.com/watch?v=R4yB-8tB0J0

http://www.fortran90.org/src/best-practices.html#interfacing-with-python

Python Solutions


Solution 1 - Python

> This seems to work but I have questions. > > 1. Is it generally good practice to mix setuptools and numpy.distribute? > 2. Does the order I import them matter? Should I always import setuptools first? > 3. Is there an official up-to-date tutorial for packaging extensions to numpy? Perhaps even one with some discussion Fortran extensions?

  1. You should not need to use numpy.distribute anymore.

  2. ^^ Not necessary

  3. Particularly for wrapping fortran code with numpy, there is the popular f2py. However I personally find the necessary code annotations redundant, because good fortran code contains all necessary information.

(warning personal project plug below)

Recently released is the cleaner fmodpy, which automatically generates all necessary wrapper code in an understandable and clean interface. It supports pre-Fortran90, but is best suited for Fortran90 and later. It could be used to generate a clean distribution along with the python interface of code (presuming users have gfortran installed).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWill MartinView Question on Stackoverflow
Solution 1 - PythonThomas LuxView Answer on Stackoverflow