package pyml_bindgen

  1. Overview
  2. Docs
Generate pyml bindings from OCaml value specifications







Published: 02 Aug 2022


OCaml-Python Bindings Generator

Generate Python bindings with pyml directly from OCaml value specifications.

While you could write all your Python bindings by hand, it can be tedious and it gets old real quick. While pyml_bindgen can't yet auto-generate all the bindings you may need, it can definitely take care of a lot of the tedious and repetitive work you need to do when writing bindings for a big Python library!! 💖

Quick start

First, install pyml_bindgen. It is available on Opam.

$ opam install pyml_bindgen

Say you have a Python class you want to bind and use in OCaml. (Filename:

class Adder:
    def add(x, y):
        return x + y

To do so, you write OCaml value specifications for the class and methods you want to bind. (Filename: val_specs.txt)

val add : x:int -> y:int -> unit -> int

Then, you run pyml_bindgen.

$ pyml_bindgen val_specs.txt adder Adder --caml-module Adder >

Now you can use your generated functions in your OCaml code. (Filename:

open Lib

let () = Py.initialize ()

let result = Adder.add ~x:1 ~y:2 ()

let () = assert (result = 3)

Finally, set up a dune file and run it.

 (name run)
 (libraries pyml))
$ dune exec ./run.exe


For information on installing and using pyml_bindgen, check out the docs.

Additionally, you can find examples in the examples directory. One neat thing about these examples is that you can see how to write Dune rules to automatically generate your pyml bindings.

You may also want to check out my blog post introducing pyml_bindgen.

Installing from sources

If you want to install from sources, e.g., to track the main branch or a development branch, but you do not want to install all the test and development packages, clone the repository, checkout the branch you want to follow and install:

$ git clone
# Checkout whatever branch you want, in this case `dev`.
$ git checkout dev
$ make opam_install

This will save a lot of install time as it avoids some heavy packages.


If instead, you want to work on pyml_bindgen development, will need to ensure you have the development and test dependencies.


$ git clone
$ make deps
$ make deps_dev
# Start working!
$ dune test


Copyright (c) 2021 - 2022 Ryan M. Moore.

Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This program may not be copied, modified, or distributed except according to those terms.

Dependencies (10)

  1. ocaml >= "4.08.0"
  2. stdio >= "v0.12"
  3. re >= "1.10.0"
  4. ppx_string >= "v0.12"
  5. ppx_sexp_conv >= "v0.12"
  6. ppx_let >= "v0.12"
  7. cmdliner >= "1.1.0"
  8. base >= "v0.12.0" & < "v0.16.0"
  9. angstrom >= "0.15.0"
  10. dune >= "3.0"

Dev Dependencies (9)

  1. odoc with-doc
  2. shexp >= "v0.14" & with-test
  3. pyml with-test
  4. ppx_expect >= "v0.12" & with-test
  5. ppx_inline_test >= "v0.12" & with-test
  6. ppx_assert >= "v0.12" & with-test
  7. ocamlformat >= "0.23" & < "0.24" & with-test
  8. base_quickcheck >= "v0.12" & with-test
  9. conf-python-3-dev >= "1" & with-test

Used by