Using Raku in Python

TL;DR:

$ pip install rakulang

$ python
Python 3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import rakulang
>>> rakulang.interpreter().eval("sub postfix:<!>($n) {[*] 1..$n}; say 5!")
120
True
>>>

Raku in a Python program

In other words, you can now use Raku in your Python programs. For that, no engine — neither Raku++, nor Rakudo, nor mutsu — has to be installed on the computer.

All you need to do is install the rakulang package from PyPI (the Python Package Index):

$ pip install rakulang

On Ubuntu and Debian you may need to create a virtual environment first:

$ python3 -m venv ~/raku-env
$ . ~/raku-env/bin/activate
$ pip3 install rakulang

Once that’s done, you get a fresh version of the Raku++ compiler, and it’s available in rakulang.interpreter():

import rakulang

raku = rakulang.interpreter()

You can now call either eval() or call() to access all the perks of Raku. For example:

import rakulang

raku = rakulang.interpreter()

raku.eval('say "Hello from Raku!"')

print(raku.eval("(1..10).sum")) 
print(raku.eval("2 ** 100")) 
print(raku.eval("<apple banana cherry>.map(*.uc)"))

Note that you can print with both Raku’s say and Python’s print.

Rats, user-defined postfixes, grammars, — it’s all unlocked:

import rakulang

raku = rakulang.interpreter()
raku.eval("say 0.1 + 0.2 - 0.3") # exact 0

BigInts? Here you go:

import rakulang

raku = rakulang.interpreter()
print(raku.eval("12**2026")) # A 2187-digit number

Functions can also be called with call:

import rakulang

raku = rakulang.interpreter()

raku.eval("""
    sub area($w, $h)   { $w * $h }
    sub total(@prices) { @prices.sum }
    sub describe(%p)   { "%p<name> costs %p<price>" }
    sub hello($name, $greeting = 'Hello') { "$greeting, $name!" }
""")

print(raku.call("area", 3, 4))
print(raku.call("total", [1, 2, 3.5]))
print(raku.call("describe", {"name": "tea", "price": 3}))
print(raku.call("hello", "Ada"))
print(raku.call("hello", "Ada", "Hi"))

Raku Grammars in Python

And finally, grammars! Of course, if you have Raku, you have great Raku grammars!

You can read a grammar’s source from a multi-line string, but you can also read it from a separate file. Say you’ve got a grammar for keeping track of your purchases in shopping.raku:

grammar Shopping {
    rule  TOP  { <item>+ }
    rule  item { <name> '=' <qty> }
    token name { \w+ }
    token qty  { \d+ }
}

class ShoppingActions {
    method item($/) { make $<qty>.Int }
    method TOP($/)  { make $<item>.map(*.made).sum }
}

This grammar Shopping comes together with the actions class ShoppingActions.

Now, use it in a Python program:

import rakulang

shopping = rakulang.Grammar.from_file(
    "shopping.raku", name="Shopping", actions="ShoppingActions")

with open("list.txt", encoding="utf-8") as f:
    m = shopping.parse(f.read())

for item in m["item"]:
    print(item["name"].str(), item["qty"].int())

print("total:", m.made)

Given some data in the list.txt, even it’s badly formatted:

milk=2  bread = 1
    eggs=12

Now run the program and get the totals:

$ python shopping.py
milk 2
bread 1
eggs 12
total: 15

Links

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code