skillfed

asteval

Safe, minimalistic evaluator of python expression using ast module

asteval v1.0.9 6.3M downloads/30d#1,945 on PyPI220
Permissive license MIT Active released

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 asteval

uv

uv add asteval

poetry

poetry add asteval

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: Implementation :: PyPy

Tags

safe python expression evaluatorrestricted python interpreterast-based code evaluationembedded scripting languageuser input evaluationmathematical expression parsersandboxed python execution
sandboxed-executionexpression-evaluationembedded-scripting

More Software Development packages