Using Python libraries in Racket
<p>Can I use Python code and libraries in Racket?  I have installed PyonR (<a href="https://github.com/pedropramos/PyonR" rel="nofollow">https://github.com/pedropramos/PyonR</a>) in DrRacket so I can choose "#lang python" and run Python code. But how can I combine Racket and Python language codes for my application?</p>

<p>There is also a limited Python to Lisp translator at <a href="https://github.com/nurv/pnil" rel="nofollow">https://github.com/nurv/pnil</a> . Is there something similar for Racket?</p>

<hr>

<p>Edit: As advised in comments, I tried following. This python code in file "pysamples.rkt" works well in DrRacket: </p>

<pre><code>#lang python

def greet(name):
    print 'Hello', name

greet('Alfred')
</code></pre>

<p>Output: </p>

<pre><code>Hello Alfred
</code></pre>

<p>I tried using above definition in Racket code, but it did not work. Following is the Racket code: </p>

<pre><code>#lang racket
; (require python/config) (enable-cpyimport!) ; ran this once; worked.

(#%require "pysamples.rkt")
(greet "Racket_code")
</code></pre>

<p>The error is: </p>

<pre><code>greet: unbound identifier in module in: greet
</code></pre>

<p>The PyonR project is the closest ready-to-use way of using Python libraries from Racket that I know of. However note that there is a difference between Python libraries written in Python and Python libraries that are a thin Python layer on top of a C library. As you have experienced the latter type is not working (to my knowledge at least - but Pedro is the one to ask).</p>

<p>If you need to use a library written in language X (for X could be Python) you can always write a "listener" program in language X that waits for messages from a Racket program, and when a message is received, computes an answer and sends it back to the Racket program. How to send and receive messages is up to you, but a simple option is to have two files, one "R-to-X" which Racket writes to and X reads from, and another "X-to-R" where Racket receives the messages.</p>

<p>This approach has some overhead, but if the computation takes longer than sending the message, then it is a viable solution.</p>

python racket ffi