Using Python Libraries in Racket

Is it possible to use Python code and libraries in Racket? I installed PyonR ( https://github.com/pedropramos/PyonR ) in DrRacket, so I can select "#lang python" and run the Python code. But how can I combine Racket and Python language codes for my application?

There is also a limited translation of Python into Lisp at https://github.com/nurv/pnil . Is there something similar for Racket?


Edit: As recommended in the comments, I tried to follow. This python code in the "pysamples.rkt" file works well in DrRacket:

#lang python

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

greet('Alfred')

Output:

Hello Alfred

I tried using the above definition in Racket code, but that didn't work. Below is the Racket code:

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

(#%require "pysamples.rkt")
(greet "Racket_code")

Error:

greet: unbound identifier in module in: greet
+4
3

PyonR - Python Racket, . , Python, Python Python, Python C. , ( , - - , ).

, X ( X Python), "" X, Racket , Racket. , , : "R-to-X", Racket , X , "X-to-R", Racket .

, , , .

+4

readme python 2.7, cpyimport. :

#lang python
cpyimport numpy as np
from "racket" import time

    def add_arrays(n):
        result = np.zeros((100,100))
        for i in range(n):
            result += np.random.randint(0, 100000, (100,100))
        return result

    print time(add_arrays(10000))

, python, , #lang python . .

+4

The previous answers and comments dealt with difficulties with some Python libraries, but if you are just interested in using a function from a pure Python file in the Racket module, try something like this:

In the file "greetings.py":

#lang python

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

In Racket:

#lang racket

(require python)
(py-import "greetings" as python-module)

(py-method-call python-module "greet" "Racket")
+2
source

Source: https://habr.com/ru/post/1652691/


All Articles