Safe Python Expression Evaluator

1.0.8 · active · verified Thu Apr 09

asteval provides a safe, minimalistic, and flexible Python expression evaluator built on the Abstract Syntax Tree (AST) module. It is designed to evaluate untrusted code within a sandboxed environment, offering fine-grained control over available symbols and operations. The current version is 1.0.8, with a development cadence focused on security enhancements, bug fixes, and supporting newer Python versions.

Warnings

Install

Imports

Quickstart

Initialize an `Interpreter` instance, then use its callable interface or `eval()` method to execute Python expressions. Variables defined persist within the interpreter's symbol table, which can be accessed via `aeval.symtable`.

from asteval import Interpreter

aeval = Interpreter()

# Define variables and execute expressions
aeval("x = 1 + 2")
aeval("y = x * 3")

# Access results from the symbol table
result = aeval.symtable['y']
print(f"Result: {result}")

# Evaluate a direct expression
direct_result = aeval.eval("10 * x + y")
print(f"Direct evaluation: {direct_result}")

view raw JSON →