I found a library that can embed Python in Common Lisp called burgled-batteries, so I tried it. It is the memorandum.
python
$ ros --version
roswell 17.5.8.78
$ ros run -- --version
SBCL 1.3.18
$ python --version
Python 2.7.13
Installation of burgled-batteries
is easy from roswell.
$ ros install burgled-batteries
Let's call the function from the following module.
hello.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def hello(name):
print "Hi, {}! This is Python.".format(name)
Below is the SBCL REPL.
Execute Python function from CL
* (ql:quickload :burgled-batteries)
To load "burgled-batteries":
Load 1 ASDF system:
burgled-batteries
; Loading "burgled-batteries"
...
(:BURGLED-BATTERIES)
* (in-package :burgled-batteries)
* (startup-python) ;;Launch the Python interpreter
* (import "sys") ;;Import sys module
0
* (run "sys.path.append('./')") ;;Added because it does not search the current directory for some reason when importing
;;run runs the embedded string in the current interpreter
* (import "hello") ;;Import hello module
* (run "hello.hello('Kojiro')") ;; hello.Call hello
Hi, Kojiro! This is Python.
Not only can it be embedded, but it can also be defined and called as a function.
Define and call a Python function in CL
* (defpyfun "hello.hello" (name)) ;; hello.hello of py hello.Defined as hello
HELLO.HELLO
* (hello.hello "Kojiro") ;;Can be called normally
Hi, Kojiro! This is Python.
Recommended Posts