# Which package should I use to wrap Modern Fortran Code with Python?

I know of, and have used f2py2e to wrap some old Fortran 77 code, but my understanding is that it does not work with newer Fortran 95 code. I've researched what I should use, and have come across fwrap and G3 f2py, neither of which seem to give any explanation of their current state, or how to use them (beyond basic usage).

I have also seen that the version of f2py has the option to use the third generation f2py, but it is commented as being non-functional. Given this, I don't know which project I should use for a uni project. Which one should I use for new code?

P.S. This is basically the same question as https://stackoverflow.com/questions/10665717/current-best-method-for-wrapping-modern-fortran-code-with-python, it was suggested that asking here might give better answers.

• "Modern Fortran" – meawoppl Jul 31 '15 at 22:01

You can use the Python builtin ctypes module as described on fortran90.org. It is pretty straight forward and doesn't require any external dependencies. Also, the ndpointer arg type helper is very handy.

• That's right. Or use Cython instead of ctypes as described on the fortran90.org page. – Ondřej Čertík May 22 '12 at 5:28
• @OndřejČertík, great website! There are few valuable resources on modern Fortran on the Internet, but this is definitely one of my favourites along with fortranwiki.org – astrojuanlu May 30 '12 at 11:22

I personally use f2py for this. However, the Fortran I write for f2py is not really modern Fortran, but rather free-form F90 with modules. I do not use assumed shapes or custom types. This method still works very well for me.

There is a resource I want to share concerning the use of C as part of the wrapping. The rest of this page is recommend, too.

• 2015 update: since quite a while, all my Fortran interfacing happens through Cython and iso_c_binding. This allows to keep the Fortran code itself modern while exposing only what's really needed to Cython/C. – AlexE Jul 30 '15 at 14:51
• 2016 update: the iso_c_binding option is getting easier now than I can just compile a .pyx file using the cython command, and reducing the overlap between changing either my fortran code or python code and quickly getting everything to work. – cbcoutinho Oct 29 '16 at 4:22

The documentation of f2py is really not that great. I'm in the process of moving my own projects from f2py to Cython. Even though Cython can be used to wrap C code, I found that it is most useful for creating new high performance code. If your intent is to write new Fortran code, wrap it, and call it in Python, I recommend that you use Cython instead. That way you can write Python code, and if there are performance bottlenecks, you transfer that over to Cython with minor modifications.

• So rather than use Fortran use Cython for the slower bits of code? – James Tocknell May 26 '12 at 3:17
• Exactly! I've found that it's much easier because your Cython code can interact with Python objects and Numpy arrays naturally (without an extra conversion layer). This is specially helpful if you want to use your own data structures. Making Fortran or C use you Python classes in f2py is not such a straight-forward task. – Leo Uieda Jun 5 '12 at 17:35

f90wrap is build on top of f2py and automatically generates the Python wrappers for the underlying Fortran code base, including access to derived types. A set of simple examples show how it works.

You could use FortWrap to get you halfway there. This will create a C++ interface to your Fortran code, which could then be wrapped with SWIG to get a Python interface. We do this on a somewhat large base of Fortran code and it works quite well (in fact, FortWrap was developed by a member of our team for this exact purpose).

fwrap specifically targets Fortran90/95 and uses some of the same software stack as f2py.

• The last commit to this project's GitHub repo was late 2010. – Geoff Oxberry Apr 18 '13 at 5:31
• While I agree that that's discouraging I've still had decent success with it. Out of the box it's more functional than f2py on F90. It also has a very low entry barrier. I was glad to have found it. – MRocklin Apr 18 '13 at 12:20

f2py supports modern fortran (assumed-shaped arrays and so), see an example script

Also, for experiments with fortran code I'd recommend IPython magic, which is easy to install and very convenient (it relies on f2py as well).

Here is a list of Fortran wrappers for Python:

https://github.com/mgaitan/fortran_magic/issues/4