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.
| 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.
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.
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.
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)
More control, slightly more work.
iso_c_binding module to create a C-compatible interface.so on Linux, .dll on Windows)iso_c_binding)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.
A Python package specifically designed to make Fortran easy to call from Python.
Simplest but least elegant.
execute_command_line()This avoids complex linking but is slower and less flexible.
For advanced use cases.
bind(C)For parallel/distributed computing.
| 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 |
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.