PythOnLisp

Last updated June 7th 2006, created 2nd February 2006

Python On Lisp: A project to call Python code from Lisp

Introduction

Every so often a newbie Lisper pops up on comp.lang.lisp and says "I want to make a web application. Python is really easy to get up and running but Lisp is much harder". Sometimes they get called a troll, but some people will also say they have a valid point.

Python has an official embedded API, meaning you can integrate it with a C/C++ program via linking with a dynamic link-library (DLL). Integration has to have some traits to make it really useful: There must be a way for C++ to run Python code, a way for that Python code to call back to C++ functions, and a way to pass data between them both.

What Python-on-lisp (POL) does is allow Lisp to interface with Python using the freely-available, easy to install and use, cross-platform library CFFI, which basically deals with calling DLLs from Lisp. This means you can run Python code from inside your version of Lisp. Currently tested Clisp 2.37, Allegro 7.0, Lispworks 4.4, but could work on lesser versions. For efficiency, POL also lets you define virtual modules, which Python code can call back to as if they were Python functions, which means the time taken to call the Lisp is as long as it takes to unwrap a pointer and run the actual code. For convenience, but with a slight loss of efficiency, POL can redirect the Python output (errors, printed text, etc) to a Lisp string, and Python programs can eval Lisp code, which is a more flexible, but slower, kind of callback.

You could call the project PyEval, like Lisp's eval function.

How to Use It

As this is an Alpha release, I cannot be held responsible for any damage or loss that occurs from running this Lisp program. Always start a new Lisp session in case Lisp crashes, and make a backup of any important files in the directory it resides in.

First you need to download Python from http://www.python.org/. 2.2 and 2.4 have been tested, but later versions should work fine too. python22.dll (or python24.dll, etc) will be put by the Python installer, in a place where any program can get at it. On Windows, that's in /Windows - in Linux, it'll be somewhere else. You don't need to copy the python22.dll file into the same folder as Pythonlisp.lisp, because it will find it automatically.

However, if you need to bundle Python functionality with a desktop application, you are allowed to re-distribute python22.dll (not sure about other versions) commercially and whatever of its .py libraries you need, without including the full Python distribution. But please double-check the Python license first before taking my word for it.

This has been tested on Linux, and works because it's the same DLL interface on that system. Python is pretty much the same anywhere. It's just a matter of finding out which Python lib you have. It's defined in pol.lisp (downloadable below), as libpython2.3.so.1.0, which works.

Next, you will need CFFI, and to install that you'll need, if you aren't using a Lisp that comes with it (Clisp does): ASDF.lisp, which I have included here as the license permits. You may want to hunt down ASDF from somewhere else.

So, download the latest version below, put it somewhere where Lisp can get at it, and load it.

For instruction, read readme.txt. All this stuff is in the Lisp file itself.

There are 2 ways of getting started. You can use Alexis Gallagher's wonderful ASDF-installable package (pythonlisp.tar.gz) and follow the instructions in doc/README or you can use the non-ASDF method as follows:

py:: is necessary because it's in package py

Call Pythonlisp with an extra parameter, eg (Pythonlisp "print pi" 'STRING) for all printed text during that session to be returned as a string and dumped to the console, or 'CONSOLE to output just to the console instead.

The capitalisation in PythOnLisp is not necessary in Lisp.

If you type (get-web-page "http://www.google.com/") then you'll get a string with that page's contents. You can see how simple the code is for that function.

If you (py::py-repl), you'll get a Python prompt and you can run Python code from it and get results. Anything defined will stay in memory between py-repl sessions. Don't do anything silly in the Python REPL, as it can do anything to your system that Python can. Treat it like any console.

It's worth pointing out that although Python is slower than Lisp, any 'network language''s speed is limited by the bandwidth available. This only becomes a problem when the data served is more than the CPU time can handle. In most cases (especially on modern machines), it's fast enough.

Known Bugs and Tips

For more known bugs, see pol.lisp (below).

I'm hoping to get CVS up and running so all changes are logged, etc.

Mailing Lists

Download

You can get it at pythononlisp.lisp or pythononlisp.tar.gz (which is ASDF-installable on Clisp, Lispworks and Allegro). It's undergoing Alpha testing, so there is no warranty, the BSD disclaimers apply. It's still being tested for any bad side-effects, although I believe there to be none (unless Python leaks memory, which my tests show it doesn't). Please read the documentation at the top of this lisp file or readme.txt.

CVS

You can browse our CVS repository or download the current development tree via anonymous cvs, as described here

Valid XHTML 1.0 Strict