7

I've got a large legacy program that is written in OCaml, and I'd like to be able to call some OCaml functions from my Python program.

How can I do this the easiest way?

ymn
  • 2,175
  • 2
  • 21
  • 39
  • [This page](http://proj.badc.rl.ac.uk/cedaservices/browser/ipython/IPython/frontend/html/notebook/static/codemirror/mode/ocaml/index.html) seems to suggest that there's a way to connect the [IPython notebook](http://ipython.org/) with OCaml. But since I've never done any work with interfacing python and OCaml, I can't be sure that's what you want – inspectorG4dget Dec 10 '12 at 06:14
  • 1
    This link doesn't work for me: I get error 403 "Forbidden" – ymn Dec 10 '12 at 06:55

3 Answers3

8

Directly, no. However, if you create a C API for your Ocaml library, you can call that API via. Python's ctypes module or similar. Likewise, if you expose a network service for your OCaml application, Python can call into that.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
8

You might find useful Pycaml, a Python-OCaml interface allowing to call OCaml functions from Python and vice versa.

Be sure to look for the 2011 or later version, which is based on a 2002 version.


Edit: removed link, since 10 years after my answer the hosting moved from http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=792 to sourceforge. In 10 years, new options might have emerged, for instance a quick search returns pyml.

jrouquie
  • 4,315
  • 4
  • 27
  • 43
  • This link is now broken. – Jeremy Caney Jul 15 '23 at 18:19
  • The answer did include the essential parts. Since the answer is a library, the post on Stackoverflow cannot contain all the library code. The link was indeed only provided for reference. During the 10 years this answer has been online, it might have helped the readers to find the library faster and make sure they are on the right page, so adding a link to the answer seems to have been a good idea. This answer is not a link-only answer. – jrouquie Jul 17 '23 at 09:36
  • 10 years later, the link has changed. The library is not maintained anymore. Another library, which was born 5 years after the above answer, seems to be a better choice now. I hope this answer helped people during the last 10 years (at least it got a few upvotes), but now I don't think it's worth maintaining the answer anymore. I'm upvoting the new answer (posted 5 years later) that points at the new library. – jrouquie Jul 17 '23 at 09:37
6

Pycaml is no more maintained: nowadays there is pyml and lymp to do that.

pyml: http://pyml.gforge.inria.fr lymp: https://github.com/dbousque/lymp

Both of them are available in opam (opam install pyml lymp). I have not tried them yet, but since I know the author of pyml is very competent, I would bet on that one.

ligand
  • 346
  • 3
  • 6