PyLua - Python Interpreter in Luau

PyLua - Running Python Code Inside Roblox

So I’ve been working on this silly little project called PyLua and thought I’d share it with you all. It’s basically a Python interpreter that runs entirely inside Roblox using Luau. You can check the Github here

Important note: This isn’t a Python-to-Luau compiler or anything fancy like that - it actually parses and executes Python code in real-time while your game is running

What it does

You can literally write Python code as a string and execute it inside your Roblox scripts:

local Python = require('./PyLua/python')

Python.execute([[
# This is actual Python code running in Roblox!
message = "Hello from Python!"
score = 85

if score >= 80:
    print("Grade: B or better!")
else:
    print("Keep studying!")

# Even loops work
for i in range(5):
    print("Count:", i)
]])

Current features (that actually work!)

  • Variables and math - Assignment, arithmetic (+, -, *, /)
  • Control flow - if/elif/else statements, for and while loops (with proper nesting!)
  • Built-in functions - print(), len(), str(), int(), range(), enumerate(), min(), max(), sum(), etc.
  • Data structures - List literals like [1, 2, 3] and even nested lists [[1, 2], [3, 4]]
  • String operations - Character iteration, length calculation
  • Tuple unpacking - for i, item in enumerate(list): works just like real Python!
  • Comments - Python-style # comments

Architecture

I went full nerd mode and made it modular:

  • Tokenizer for lexical analysis
  • Parser for syntax trees
  • Evaluator for expressions
  • Separate modules for control flow, variables, and built-ins

Why did I make this?

Honestly? Just for the fun of it! I wanted to see if I could build a working interpreter in Luau, and it turned out to be a pretty awesome learning experience. Its easy to add new features, so I might keep expanding it whenever I’m bored.

What’s next?

Still working on:

  • Function definitions (def)
  • Dictionaries
  • More built-in functions
  • Maybe classes eventually?

Now go and check the Github maybe even download it? You chose, bye :3

34 Likes

Would be cool to be used as a programming language for modding inside the expirience maybe.

4 Likes

could be cool yeah, like other programs use lua for plugins. I just need to make it good enough for it to be considered good to use :slight_smile:

I’ve always wanted to make an OS with programmable apps. This’ll defo help me to understand how to make a custom programming language and run it in roblox. Awesome work :slight_smile:

1 Like

Thanks! Feel free to use some of the code, its under the MIT license so go for it.

You could gain a lot of performance here by targetting .pyc and running off that. Theres some interesting stuff out there with bytecode virtual machines, especially pyc. If you ever want to dip your hands into full on compilation, give it a try. CPython Internals is a good starting point imo.

You’d also allow scripts compiled from the outside world to run in your interpreter (not including library support) if your virtual machine correctly maps to each operation in the bytecode format.

Other than that, cool library.

This is actually very interesting, I’ll take a look at pyc and decided if its worth the try, thanks!

Okay so, I actually looked into the idea of running raw .pyc files directly and dove deep into CPython’s internals. While it’s a cool concept, I decided not to fully replicate Python’s bytecode for a few key reasons:

  • Most games do not want to support dynamic code loading (like loadstring) and roblox luau does not support file access, and its memory model is tightly controlled.
  • Python bytecode (.pyc) is deeply tied to CPython’s internals (like its C-based memory management and stack frames), which don’t map cleanly to Luau’s VM.
  • Supporting full .pyc would introduce a ton of complexity for limited benefit — especially since the target environment is so different.

Instead, I’m taking inspiration from the concept and designing a simplified bytecode format for which allows:

  • Faster loading (compiled once, run multiple times),
  • Better performance than interpreting raw strings every time,
  • Safer and more controlled execution for in-game scripts and mods.

This also opens up AOT workflows (compile on PC, run on Roblox) while still supporting direct string-based execution for development.

In short: I’m taking .pyc, but optimizing it for Roblox and Luau’s limitations — and I think it is a good balance between them. Thanks again for the suggestion!

1 Like

Bytecode executor has mostly all core features, have around 80% of opcodes defined and working, more inside PyLua/src/PyLua0.2/vm, going to start working on finalizing it and then moving on to the Bytecode Compiler. I’m open for feedback on the opcodes before I start on it :3

PyLua 0.2

Took me some time to make it work but the base is done :slight_smile:

Features

  • Bytecode Compilation: Compile Python code once, execute multiple times
  • Python Syntax: Almost full support for all Python variables, operators, control flow, and data structures
  • Data Structures: Lists, dictionaries, tuples, sets with proper Python semantics
  • Control Flow: if/elif/else, for loops, while loops with nested support
  • Built-ins: print(), len(), type(), range(), int(), float(), str(), bool()
  • Optimized VM: Stack-based virtual machine for efficient bytecode execution

All features detailed here

Basic Usage

local python = require('path.to.PyLua0.2.python')
-- Simple one-line execution
python.execute('print("Hello, PyLua 0.2!")')

Compile Once, Execute Multiple Times

For better performance when running the same code repeatedly:

local python = require('path.to.PyLua0.2.python')

-- Compile Python code to bytecode
local bytecode, error = python.compile([[
x = 10
y = 20
result = x + y
print("Result:", result)
]])

if error then
    print("Compilation error:", error)
    return
end

-- Execute the bytecode multiple times
for i = 1, 5 do
    print("Execution", i)
    local success, variables = python.runBytecode(bytecode)
    if success then
        print("Variables:", variables)
    end
end

Check out the demo file with all features, download PyLuaV0.2 or just check the Github repository.