PyonR (pronounced "Pioneer") is an implementation of the Python programming language for the Racket platform.
You can install PyonR from DrRacket's Install Package dialog box with
git://github.com/pedropramos/PyonR as the package source. Make sure you change the package name to
Alternatively, you can install it with the
raco tool by running:
raco pkg install -n python git://github.com/pedropramos/PyonR
You can update the currently installed version with DrRacket's Package Manager, or alternatively with the following raco command:
raco pkg update python
To use PyonR with DrRacket, simply replace
#lang racket with
PyonR currently supports most of the Python language as specified by version 2.7, except for a few constructs (most notably the
with statements, decorators and the
super function for constructors) and some missing methods on built-in types.
Extensions to the Python language:
Importing modules from Python 2.7
If you have Python 2.7 installed on your system, you can import its modules using Python's importing statements, but replacing the
import keyword by
cpyimport. This allows you to access Python's full standard library as well any third-party libraries you have installed, such as NumPy or SciPy.
For a demonstration, check out the examples at
Many users have reported issues with CPython's VM during PyonR's installation, therefore the
cpyimport import must now be manually enabled from Racket after installing PyonR.
To do so, require the
python/config module and run
(enable-cpyimport!). You only need to do this once.
If you turn out to have any issues after this, you can disable it by running
Importing modules from Racket:
You can import Racket modules by using Python's
import statements and specifying the module's require specification withing string quotes. For instance, to access Racket's
cdr functions from Python, one could type one of the following:
import "racket" as rkt
from "racket" import cons, car, cdr
from "racket" import *
Keep in mind that Python's rules for identifier names are stricter than Racket's. Therefore, PyonR resorts to name mangling for identifiers whose names are not allowed in Python. For instance, the
number->string function would have to be imported as:
from "racket" import number_TO_string
We provide a builtin Python module called
name_mangling with the function
mangle which returns a Python mangled name, given the original Racket name as argument.
PyonR supports an extensible predicate dispatch mechanism to map Racket predicates to Python types. This is particularly aimed for giving a Python feel to imported Racket datatypes, since their functionality is typically spread among predicates, constructors, getters, setters and assorted functions. This way, you can map a datatype's predicate to a user-defined Python class such that getters and setters are available as properties and other functions are available as methods.
This functionality is provided by the builtin Python module
predicates which defines the following functions:
set_predicate(pred, type)- sets a mapping between the
define_subtype(pred, parent)- defines the
predpredicate as a subtype of
parentpredicate, so that pred is dispatched before parent.
For a demonstration, check out the example at