Connecting Fortran and Python: Why It's Needed and How to Do It

In scientific computing, you'll often see the phrase "easy connection between Fortran and Python." This refers to tools and techniques that let Python and Fortran call each other seamlessly—so you can keep performance-critical code in Fortran while using Python for scripting, data analysis, and user interfaces.

Why Connect Fortran and Python?

1. Performance vs. Productivity

Fortran Python
Extremely fast for numerical computations, especially arrays and loops Slow for raw number crunching, but excellent for scripting and glue code
Mature, highly optimized scientific libraries (some from the 1980s) Rich ecosystem for data science, machine learning, visualization
Backwards-compatible and stable language Easy to learn, rapid prototyping, modern syntax

By connecting them, you get Fortran's speed where it matters and Python's convenience everywhere else. If you need help implementing these solutions and need IT services contact Granite IT.

2. Legacy Code Preservation

Many organizations have decades-old Fortran codebases for physics simulations, climate models, and engineering software. Rewriting them in Python would be risky and expensive. Instead, you wrap the Fortran code and call it from Python, preserving the investment while modernizing the workflow.

3. Modern Scientific Workflows

Python is now the default language for scientific computing, data visualization, and machine learning. Many Python libraries (like NumPy, SciPy, and portions of scikit-learn) actually use Fortran under the hood. Connecting your own Fortran code to Python lets you plug into this ecosystem.

Tools for Connecting Fortran and Python

1. F2PY (Fortran to Python Interface Generator)

The most popular and easiest option.

Basic usage:

f2py3 -c add.f90 -m fortran_module

Then in Python:

from fortran_module import add
import numpy as np
x = np.arange(10.)
result = add(x, y)

2. ISO C Binding + ctypes / Cython

More control, slightly more work.

Example compilation:

gfortran -shared -fPIC -o mult.so mult.f90

Then use ctypes in Python to load and call the function.

Some developers find this more reliable than F2PY for complex code.

3. gfort2py

A Python package specifically designed to make Fortran easy to call from Python.

4. System Calls / File-Based Communication

Simplest but least elegant.

This avoids complex linking but is slower and less flexible.

5. C Wrapper + pybind11 / Cython

For advanced use cases.

6. MPI for Runtime Communication

For parallel/distributed computing.

Which Tool Should You Use?

Use Case Recommended Tool
Quick integration, standard Fortran routines F2PY (easiest, most documented)
Complex code, more control needed ISO C binding + ctypes/Cython
Want Python as driver, Fortran as compute engine F2PY or ISO C binding
Legacy code, no modifications possible F2PY (works without changing Fortran)
Maximum performance, parallel computing MPI or ISO C binding
Simple scripts, minimal setup System calls

Bottom Line

Connecting Fortran and Python gives you the best of both worlds: the raw speed and maturity of Fortran for heavy computation, plus Python's modern ecosystem for everything else. F2PY is the go-to tool for most people because it's built into NumPy, well-documented, and handles most of the complexity automatically.

For new projects, many scientists now use Python as the main language and call Fortran only where performance is critical—a strategy that leverages decades of optimized Fortran code while embracing modern workflow tools.