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.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
build/bin
tests
.appveyor.yml
.codeclimate.yml
.coveragerc
.flake8
.gitattributes
.gitignore
.travis.yml
CMakeLists.txt
LICENSE.txt
MANIFEST.in
README.md
badcomment.f
badcomment_implicit.f
f2py_demo.py
floatPrecision.f90
floatPrecision.py
goodcomment.f
goodcomment.f90
mypy.ini
prod.f90
pyproject.toml
setup.cfg
setup.py

README.md

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.

Build

Fortran compiler is needed:

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

Install

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

Examples

Fortran Intents

python f2py_demo.py

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.