Skip to content
Examples of using f2py to get high-speed Fortran integrated with Python easily
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
build/bin meta Jun 11, 2019
.appveyor.yml f2py and CI template Jun 11, 2019
CMakeLists.txt cmake Jun 14, 2018
badcomment_implicit.f init Apr 3, 2017 cleanup Jun 14, 2018
goodcomment.f90 init Apr 3, 2017
setup.cfg rename [skip ci] Jun 11, 2019

Build Status Coverage Status Build status Maintainability

f2py Examples

Simple examples of using f2py to get high-speed Fortran integrated with Python easily. These examples are also useful to troubleshoot problems with f2py.


Fortran compiler is needed:

  • Mac: brew install gcc
  • Linux: apt install gfortran or yum install gfortran
  • Windows


pip install -e .

This will compile the Fortran code (in .f and .f90 files). It creates a file pyprod.* where * depends on operating system and Python version:

  • Linux/Mac: .so
  • Windows: .pyd


Fortran Intents


You will see the output:

x = 3
y = 2
x * y = 6.0
Your system did this in Python using Fortran-compiled library

Fortran comment syntax

Fortran 77 is officially full-line comments only. Inline comments are not allowed with f2py as a result in Fortran 77 files. Demonstrate this with:

f2py -m badcomment -c badcomment.f

Troubleshooting f2py

f2py normally Just Works on Linux, Mac and Windows Subsystem for Linux. However, Windows itself can be more challenging due to inconsistencies in Microsoft Visual Studio.

See the Windows f2py installation guide and troubleshooting guide.

You can’t perform that action at this time.