Myokit contains a module with functions and classes that are used throughout Myokit, but are not particularly Myokit-specific. This module is imported automatically when import myokit is run.



Provides reasonably precise benchmarking (with times obtained from timeit.default_timer()) and formats times.


m,p,x = myokit.load('example')
s = myokit.Simulation(m, p)
b =

Formats a (non-integer) number of seconds, returns a string like “5 weeks, 3 days, 1 hour, 4 minutes, 9 seconds”, or “0.0019 seconds”.

If no time is passed in, the value from time() is used.


Prints a message to stdout, preceded by the benchmarker time in us.


Resets this timer’s start time.


Returns the time since benchmarking started (as a float, in seconds).

Capturing printed output

class, enabled=True)

Context manager that temporarily redirects the current standard output and error streams, and captures anything that’s written to them.


with as a:
    print('This will be captured')

Within a single thread, captures can be nested, for example:

with as a:
    print('This will be captured by a')
    with as b:
        print('This will be captured by b, not a')
    print('This will be captured by a again')

Capturing is thread-safe: a lock is used to ensure only a single thread is capturing at any time. For example, if we have a function:

def f(i):
    with as a:

    return a.text()

and this is called from several threads, the capture acts as a lock (a threading.RLock) so that one thread will need to finish executing the code within the with statement before a second thread can start capturing.

In multiprocessing, no locks are used, and no memory or streams are shared so that this should also be safe.

By default, this method works by simply redirecting sys.stdout and sys.stderr. This captures any output written by the Python interpreter, but does not catch output from C/C++ extensions or subrocesses. To also catch that output start the capture with the optional argument fd=True, which enables a file descriptor duplication method of redirection.

To easily switch capturing on/off, a switch enabled=False can be passed in to create a context manager that doesn’t do anything.


Returns the text captured from stderr, or an empty string if nothing was captured or capturing is still active.


Returns the text captured from stdout, or an empty string if nothing was captured or capturing is still active.


Returns the combined text captured from output and error text, if any (output first, then error text).

File system, root='.')

Formats a path for use in user messages. If the given path is a subdirectory of the current directory this part is chopped off.

Alternatively, a root directory may be given explicitly: any subdirectory of this path will be formatted relative to root.

This function differs from os.path.relpath() in the way it handles paths outside the root: In these cases relpath returns a relative path such as ‘../../’ while this function returns an absolute path., silent=False)

Version of shutil.rmtree that handles Windows “access denied” errors (when the user is lacking write permissions, but is allowed to set them).

If silent=True any other exceptions will be caught and ignored.

String comparison, t)

Returns the Levenshtein distance between two strings s and t.

Function to use as key in a sort, to get natural sorting of strings (e.g. “2” before “10”).

Usage examples:


variables.sort(key=lambda v:

Deprecated functions

Some of the functions in tools have deprecated aliases, that will be removed in future Myokit releases.

class myokit.Benchmarker

Deprecated alias of


Takes a dictionary of string : float mappings and returns a single multi-line string, where each line is of the form key = value, where key is padded with spaces to make the equals signs align.

This method is deprecated and will be removed in future versions of Myokit.

myokit.format_path(path, root='.')

Deprecated alias of

myokit.strfloat(number, full=False, precision=64)

Deprecated alias of myokit.float.str.