Allows python to interface with MESA
Branch: master
Clone or download
Latest commit 419c0f2 Feb 5, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
images Add logo Aug 15, 2017
mesa_models Update scripts Feb 5, 2019
patches Add support for mesa>11035 Sep 7, 2018
pydfriddr Update scripts Feb 5, 2019
.gitignore Initial commit Jul 25, 2017
CITATION Add citation data Apr 10, 2018
LICENSE Initial commit Jul 25, 2017 Add support for mesa>11035 Sep 7, 2018
codemeta.json Better doi Feb 5, 2019 Add support for mesa>11035 Sep 7, 2018 Fix buildRunStarSupport Oct 8, 2018

pyMesa logo



Allows python to interface with MESA stellar evolution code. Current stable version is 1.0.3


Note: pyMesa currently only works on linux, macs will fail to build.

gfort2py (Also available via pip) (needs version >= 1.0.11)

All versions need numpy and patch

if using MESA < 11035 then you also need:






Grab a recent (atleast the september 2017 version) sdk from:


MESA patching

Run the script from this folder, after setting MESA_DIR and initializing the SDK.

chmod u+x

This script should only be run once to setup up mesa. It can be ran again if you do a ./clean inside the MESA_DIR.

MESA < 11035

If using mesa version < 11035 then the following applies:

Adipls, gyre and stella are not currently built as part of this.

While we can use the individual MESA modules we can not currently run a full star (or binary) model, in either python or fortran.

Supported MESA versions

  • 9793
  • 10000
  • 10108
  • 10398
  • 11035 (non released mesa version)

Other versions can be supported upon request.

If you want to play with fire and try with another version, then set the environment variable:


Will override the version check and attempt to build MESA using the patches for the version specified. Things may not work between versions if MESA's build system changes.


# Set MESA_DIR and initialize the sdk

# If using MESA < 11035 set the LD_LIBRARY_PATH


The only python file that is actually needed to run this is This file contains the code needed to interface with mesa. Inside the mesa_models/ folder contain examples of how to interface with most of MESA's modules. pydfriddr/ folder contains examples for testing MESA's analytic derivatives with a numerical derivative.


Here is a basic example of talking to the const/ module.

# Just need to make sure the file is visible either in the local directory or in PYHTHONPATH
# It does not need to be in $MESA_DIR folder.
import pyMesaUtils as pym

# pyMesa module defines a number of useful MESA paths as pym.SOMETHING.
print(pym.MESA_DIR) # Print MESA_DIR
print(pym.MESA_VERSION) # Print MESA version number

# Loads the const module
const_lib,const_def = pym.loadMod("const")

# When calling a function we must either set the value we want (for intent(in/inout) variables) or an empty variable for intent(out).
# Calls a function
res = const_lib.const_init(pym.MESA_DIR,ierr)

# If the call was a subroutine then res is a dict with the intent out variables in there
# else it contains the result of the function call

# Accessing a variable defined in a module is simply:

# If the variable is not a parameter then you can change it with:
const_def.standard_cgrav = 5.0

# When passing a derived type, you should pass a dict to the function (filled with anything you want set)
x = {}
# or
x = {'a':1,'b':'abc','c':{'d':1}}

# Functions accepting arrays should pass a numpy array of the size it expects (if the function allocates the array, then just pass an array of size 1)
x = np.zeros(size)

# Arrays inside derived types are unstable at the moment and don't completely work.

Function names and module variables are all tab completable.


Remember that fortran has 1-based arrays while numpy uses 0-based. This comes up if you're accessing an array via a mesa constant:


should instead be accessed as:


An example of this can be found in

Modules that work

  • (partial support)


If using MESA < 11035:

The best bet is just to re-download mesa, during the setup phase we alter a lot of files. If you want to try to keep your MESA_DIR then something like this should work:

for i in $(ls patches/* | sort -r);
    patch -R -p1 < $i

for i in $(ls crlibm/crlibm-patches/* | sort -r);
    patch -R -p1 < $i

rm -rf $MESA_DIR/crlibm/crlibm-patches $MESA_DIR/{star,binary}/skip_test

If using MESA >= 11035, then in utils/makefile_header switch USE_SHARED=YES to USE_SHARED=NO and do a ./clean and ./mk in $MESA_DIR

Bug reports:

Bug reports should go to the issue tracker on github. Please include mesa version, gfortran version, gfort2py version and pyMesa version


In general most of the development should go towards the gfort2py project to add new fortran features. This repository just handles building mesa for python support.

Bug reports, if mesa versions don't work, or new examples are welcome as either pull requests or issues on the github tracker.


People who use pyMESA in papers should cite this using the zenodo link for the version they used. If you use pyMesa in a project (research or teaching), let me know and i can help advertise here (also useful for me to help with funding requests). Current versions citation is in the CITATION file.

Known Projects using pyMesa

Poelarends et al 2017