asteval
Safe, minimalistic evaluator of python expression using ast module
What it is and what it does
Asteval is a restricted Python interpreter that safely evaluates expressions and statements by parsing them into Python's abstract syntax tree (AST) and walking the tree to compute results. It supports a large subset of Python—conditionals, loops, comprehensions, f-strings, standard data structures, and math/NumPy functions when available—while intentionally blocking unsafe operations like class creation, module imports, eval/exec, and access to private methods. This makes it suitable for embedding in applications that need to accept user-written formulas or scripts without the security risks of Python's eval().
The package has no external runtime dependencies and works with Python 3.10 and later, including PyPy. It emphasizes mathematical expressions, so NumPy functions are automatically available if NumPy is installed. Programmers can inject custom functions and data into each Asteval session, and users can define their own functions within the language's constraints. The AST-based approach guarantees parsing identical to Python's, making correctness straightforward to verify.
Use it for:
- Embed a formula or macro language in a GUI or web application to let users define calculations without exposing eval() risks.
- Build a simple calculator or expression evaluator for mathematical or scientific workflows.
- Parse and execute user-supplied configuration or rule definitions in a controlled sandbox.
- Evaluate conditional expressions or transformations in data processing pipelines.
- Prototype or test Python logic without running untrusted code in the main interpreter.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Asteval is a safe Python expression and statement evaluator that uses the ast module to interpret user input without the risks of Python's built-in eval().
Yes. Asteval is a mature, actively maintained package (latest release 2026-06-11, no known vulnerabilities) with zero runtime dependencies and low install friction. It solves a real security problem—safe evaluation of user input—and is well-suited for embedded scripting, mathematical evaluation, or any scenario where you need Python-like syntax without the risks of eval(). The MIT license is permissive. Install it if you need to accept and execute user-written expressions safely.
Install
asteval on PyPI
pip
pip install astevaluv
uv add astevalpoetry
poetry add astevalInstalling asteval
Before you install
Low friction: pure Python wheel with no runtime dependencies. Actively maintained as of 2026-08-11, supports Python 3.10 through 3.14 and PyPy. No known vulnerabilities.
License in practice
MIT license permits commercial and private use with minimal restrictions—include a copy of the license and attribution when distributing.
Quickstart
pip install asteval
from asteval import Interpreter
interp = Interpreter()
result = interp('2 * x + 1', x=5)
print(result) # 11
Requires Python 3.10 or higher.
Verify before relying
- Whether NumPy integration is automatic or requires explicit configuration when NumPy is installed.
- Performance characteristics when evaluating complex or deeply nested expressions.
- Whether custom function registration persists across multiple eval() calls in the same Interpreter instance.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 64 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 6,261,626/month — #1,945 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: asteval-1.0.9-py3-none-any.whl
Keywords: AST, expression evaluation, eval
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
levalLeval evaluates limited expressions safely by…
permissive · top 15,000 on PyPI
pure-evalSafely evaluate Python AST nodes without…
permissive · top 1,000 on PyPI
py-expression-evalParses and evaluates mathematical expressions…
permissive · top 15,000 on PyPI
simpleevalEvaluates mathematical and logical expressions…
permissive · top 5,000 on PyPI
cel-expr-pythonA Python wrapper for the CEL C++ implementation…
permissive · top 15,000 on PyPI
cel-pythonParses and evaluates Google Common Expression…
unclear · top 5,000 on PyPI
starlark-pyo3Exposes the Starlark interpreter (a Python-like…
unclear · top 15,000 on PyPI
RestrictedPythonRestrictedPython compiles and executes a…
unclear · top 5,000 on PyPI
common-expression-languageEvaluates Google's Common Expression Language…
permissive · top 15,000 on PyPI
math-verifyEvaluates mathematical expressions from LLM…
permissive · top 5,000 on PyPI