Skip to main content
`Ctrl`+`K`

# F2PY user guide and reference manual

The purpose of the `F2PY` –Fortran to Python interface generator– utility is to provide a connection between Python and Fortran. F2PY distributed as part of NumPy (`numpy.f2py`) and once installed is also available as a standalone command line tool. Originally created by Pearu Peterson, and older changelogs are in the historical reference.

F2PY facilitates creating/building native Python C/API extension modules that make it possible

- to call Fortran 77/90/95 external subroutines and Fortran 90/95 module subroutines as well as C functions;

- to access Fortran 77 `COMMON` blocks and Fortran 90/95 module data, including allocatable arrays

from Python.

Note

Fortran 77 is essentially feature complete, and an increasing amount of Modern Fortran is supported within F2PY. Most `iso_c_binding` interfaces can be compiled to native extension modules automatically with `f2py`. Bug reports welcome!

F2PY can be used either as a command line tool `f2py` or as a Python module `numpy.f2py`. While we try to provide the command line tool as part of the numpy setup, some platforms like Windows make it difficult to reliably put the executables on the `PATH`. If the `f2py` command is not available in your system, you may have to run it as a module:

    python -m numpy.f2py

Using the `python -m` invocation is also good practice if you have multiple Python installs with NumPy in your system (outside of virtual environments) and you want to ensure you pick up a particular version of Python/F2PY.

If you run `f2py` with no arguments, and the line `numpy Version` at the end matches the NumPy version printed from `python -m numpy.f2py`, then you can use the shorter version. If not, or if you cannot run `f2py`, you should replace all calls to `f2py` mentioned in this guide with the longer version.

For Meson build examples, see Using F2PY.

- F2PY user guide
  - Three ways to wrap - getting started
    - The quick way
    - The smart way
    - The quick and smart way
  - Using F2PY
    - Using `f2py` as a command-line tool
    - Python module `numpy.f2py`
    - Building with Meson (Examples)
  - F2PY examples
    - F2PY walkthrough: a basic extension module
    - A filtering example
    - `depends` keyword example
    - Read more
- F2PY reference manual
  - Signature file
    - Signature files syntax
  - Using F2PY bindings in Python
    - Fortran type objects
    - Scalar arguments
    - String arguments
    - Array arguments
    - Call-back arguments
    - Common blocks
    - Fortran 90 module data
    - Allocatable arrays
  - F2PY and build systems
    - Basic concepts
    - Build systems
  - Advanced F2PY use cases
    - Adding user-defined functions to F2PY generated modules
    - Adding user-defined variables
    - Dealing with KIND specifiers
    - Character strings
  - Boilerplate reduction and templating
    - Using FYPP for binding generic interfaces
  - F2PY test suite
    - Adding a test
- F2PY and Windows
  - Overview
  - Baseline
  - PowerShell and MSVC
  - Microsoft Store Python paths
    - F2PY and Windows Intel Fortran
    - F2PY and Windows with MSYS2
    - F2PY and Conda on Windows
    - F2PY and PGI Fortran on Windows
- 1 Migrating to `meson`
  - 1.1 Baseline
  - 1.2 Compilation options
    - 1.2.1 Basic Usage
    - 1.2.2 Specify the backend
    - 1.2.3 Pass a compiler name
    - 1.2.4 Dependencies
    - 1.2.5 Libraries
  - 1.3 Customizing builds