f2py: Running Fortran code in Python on Windows

F2PY is a great package allowing you to run a lot of Fortran code from Python. Fortran 77 code naturally has a high chance of success.
Fortran 90 code can also work, but very elaborate Fortran 90 code may not work.

On Linux, all you need to do is

sudo apt-get install python-numpy gfortran

On Windows, the f2py setup is more involved. You need the MinGW compiler and to modify your system path. I assume here that you have Anaconda Python with Numpy 1.9 installed already. This procedure was used on a 64-bit Windows 10 machine.

1) install MinGW-W64 with the setup settings:
Version 4.9.2 (or newer)
Architecture: x86_64
Threads: posix
Exception: seh
Build revision: 2

2) Insert the mingw bin path to your system path. I did this permanently by going into Windows Advanced System Settings, Environment Variables, System variables, Path, and at the end put:

C:\Program Files\mingw-w64\x86_64-4.9.2-posix-seh-rt_v4-rev2\mingw64\bin

check to see if that’s where your gfortran.exe is at.

test your installation by say using the lowtran7 code.
Following the instructions there, you should get a lowtran7.pyd file that is invoked when running
python pylowtran7.py and you’ll see a plot of atmospheric loss.

Note: If you get the “NotImplementedError: Only MS compiler supported with gfortran on win64″, that is still occurring with Numpy 1.9.2, but has been patched in the master branch (to be released in a future version of Numpy). You can patch this yourself in c:\Anaconda\Lib\site-packages\numpy\distutils\fcompiler\gnu.py on line 337. Comment out the
raise NotImplementedError(...)
and simply replace with the word
pass
and you’ll be working.

Lowtran7 example output in the Arctic looking near zenith.
Lowtran7 example output in the Arctic looking near zenith.