[clpython-devel] Calling Python functions from Lisp [Re Franz thread: spr35431]

Stuart C. Shapiro shapiro at cse.Buffalo.EDU
Thu Jan 8 15:56:48 UTC 2009


I think that I have successfully installed CLPython.  What I want to do 
is to call  Python functions, defined in programs written by others, 
from Allegro Common Lisp.  So here is a test Python program called test.py:
-----------------------------
# A Python file to use to test clpython

def test():
    print "I am a Python function."
    return "I am done."

print("The Python test file has been run.")
-----------------------------

I loaded clpython, and did (note: this wasn't the first time I imported 
test, so test.fasl and test.pyc already existed.):
-----------------------------
cl-user(3): (clpython:run "import test; test.test()")
Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
Warning: /net/projects/shapiro/clpython/test.fasl not in
         #<equal hash-table with 0 entries>
; Fast loading /net/projects/shapiro/clpython/test.fasl
The Python test file has been run.
Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
Warning: /net/projects/shapiro/clpython/test.fasl not in
         #<equal hash-table with 0 entries>
I am a Python function.
"I am done."
-----------------------------

First question:  Why the Warning messages?  How can I get rid of them?

Notice that I can make use of the values returned by the Python function:
-----------------------------
cl-user(4): (setf x (clpython:run "import test; test.test()"))
Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
Warning: /net/projects/shapiro/clpython/test.fasl not in
         #<equal hash-table with 0 entries>
; Fast loading /net/projects/shapiro/clpython/test.fasl
The Python test file has been run.
Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
Warning: /net/projects/shapiro/clpython/test.fasl not in
         #<equal hash-table with 0 entries>
I am a Python function.
"I am done."

cl-user(5): x
"I am done."
-----------------------------

What I'd really like to do now is to call test.test() as much as 
possible as though it were a Common Lisp function.  One possible way is:
-----------------------------
cl-user(6): (clpython:run "test.test()")
Error: NameError: Variable `test' is unbound.
  [condition type: NameError]

Restart actions (select using :continue):
 0: Enter a Lisp value to use for `test'.
 1: Return to Top Level (an "abort" restart).
 2: Abort entirely from this (lisp) process.
-----------------------------
But, the name of the function does not seem to survive from one call of 
clpython:run to another.

Final question: How can I do this?

stu

-- 
Stuart C. Shapiro
Professor, Department of Computer Science and Engineering
University at Buffalo, The State University of New York
201 Bell Hall, Box 602000, Buffalo, NY 14260-2000, U.S.A.
PHONE:  716-645-3180x125    FAX:  716-645-3464
shapiro at cse.buffalo.edu     http://www.cse.buffalo.edu/~shapiro/





More information about the Clpython-devel mailing list