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
oryum install gfortran
- Windows
pip install -e .
This will compile the Fortran code (in .f
and .f90
files) and create
a file pyprod.*
where *
depends on your operating system and Python version.
- Linux/Mac:
.so
- Windows:
.pyd
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 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
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.