8

I need to call routines from Fortran modules within Python. I did it with f2py and python2.7. It worked pretty well.

Now, I have to use it with python3 but f2py does not seem to be compatible with python3.

I see that some people use a version called f2py3, but it is neither available through pip, not through macports (I am using a Mac). Also, python3+numpy+f2py seem to be already integrated in Fedora.

Does anyone have managed to use f2py3 (or its equivalent) with python3 on a Mac? If not, what alternate solution do you propose to link Fortran libraries with python3?

jvtrudel
  • 1,235
  • 3
  • 15
  • 28
  • I'm interested in this also. FYI, your Fedora link above returns no results when I try it but I get some info just searching for 'f2py' via your link. – JohnE Mar 05 '16 at 22:54
  • I fixed the broken link. It should point on the download pages of the RPM resource for python3-numpy-f2py. – jvtrudel Mar 07 '16 at 05:18
  • yeah, works now. thanks! – JohnE Mar 07 '16 at 05:22

2 Answers2

5

As an alternative, you can use and to call Fortran routines without relying on f2py. The downside of this approach is that you need to specify the interfaces yourself.

Here is an example I posted on SO some time ago.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Very interesting because f2py does not seem to be maintained anymore (maybe I am wrong?). The problem is that I need to use a lot of old fortran codes. Refactoring interfaces is not an easy task and can generate errors. But for sure, I'll use you suggestion for new codes. – jvtrudel May 02 '15 at 10:20
  • Well, for old code you could use compiler options to influence the generated function names (e.g. [`-fno-underscoring` for `gfortran`](https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html)) and try to call the routines from python directly (without the ISO_C_Bindings). Then all the work would need to be done on the Python side. Note that this is (in general) not portable, and depends on the compiler used! – Alexander Vogt May 02 '15 at 10:27
2

f2py comes with numpy and based on this January 2020 activity on github, it seems clear that f2py is currently maintained.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99