If you love python, have lots of legacy Fortran code, and are stuck working on a Windows platform, then this is the page for you. F2py is a tool that converts Fortran source code into python binary modules. This page is intended as a step by step instruction in getting f2py working on Windows XP.

Step One - Install Python

Chances are you've already done this. If not, the install is relatively simple. http://www.python.org/download/

Step Two - Install Numpy

Although originally developed as a standalone tool, F2PY is now included in the Numpy package. Download the the installer that matches the version of python you have installed. Again, there's nothing to the installation. http://numpy.scipy.org/

Step Three - Install gfortran

Gfortran is a Fortran95 compiler that is now part of the GCC toolkit. It is available as an easy to install package on the gfortran wiki. You want the "Native Windows" installer. http://gcc.gnu.org/wiki/GFortranBinaries

There have been reported problems with F2PY when gfortran is installed in a directory whose name has spaces, such as "Program Files". To be safe, install gfortran in C:\.

Step Four - Configure Windows Environment Variables

The gfortran installer automatically adds its "bin" directory to your path. However, for f2py to work properly, you need to set the "C_INCLUDE_PATH" environment variable to "C:\Path\to\gfortran\include"

Optional - you probably want to add your python directory to your path as well. Append the PATH variable with "C:\path\to\python2X\"

Step Five - Compile and use your first Fortran module!

Say you have a Fortran source file called foo.f90 that contains the following subroutine (Functions work as well.)

To compile your Fortran routine into a python module use the following command:

Remember to link against the correct CRT (Python 2.5 uses msvcr71.dll), f2py does not do this automatically for mingw32 (whereas distutils does). If the process is successful, the result will be a file called foo.pyd which you can import into python as shown below:

F2PY Windows (last edited 2008-09-29 21:26:53 by SturlaMolden)