skillfed

simpleeval

A simple, safe single expression evaluator library.

simpleeval v1.0.7 15.0M downloads/30d#1,206 on PyPI612
Permissive license MIT Active released

What it is and what it does

simpleeval is a lightweight library that safely evaluates mathematical and logical expressions provided as strings, without exposing the full power and security risks of Python's eval(). It uses Python's ast module to parse expressions and enforce strict limits on what operations are allowed, making it suitable for scenarios where you want to let users or configuration systems define formulas—such as setting alarm volumes based on time of day, or allowing simple math in web forms—without granting arbitrary code execution.

The library comes with sensible defaults: basic arithmetic, comparison, and bitwise operators; a handful of utility functions like randint(), int(), float(), and str(); and configurable limits on exponentiation and string length to prevent denial-of-service attacks. You can extend it by passing custom functions and variables (called 'names') into the evaluator, or by replacing operators entirely. It's deliberately kept simple and single-file, so you can even embed it directly in a project if you prefer not to add a dependency.

Use it for:

  • Allow users to define alarm volume rules that depend on time of day, alarm level, and other state without risking code injection.
  • Accept simple mathematical formulas in web forms or configuration files and evaluate them safely server-side.
  • Build rule engines or conditional logic systems where non-technical users can write expressions like 'if x > 10 then y else z'.
  • Implement dynamic pricing or discount calculations in e-commerce systems based on user-provided or admin-configured formulas.
  • Parse and evaluate mathematical expressions in scientific or educational applications without the security overhead of full eval().

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Evaluates mathematical and logical expressions from strings in a safe, sandboxed way without using Python's built-in eval(), letting you add user-configurable formulas to applications.

Yes. The package is mature, has no dependencies, carries no known vulnerabilities, and solves a real security problem—allowing safe user-defined expressions. Install it if you need to evaluate untrusted formulas; avoid it only if you need features beyond basic arithmetic, comparisons, and simple function calls, or if you're not concerned about the security implications of eval().

Install

simpleeval on PyPI

pip

pip install simpleeval

uv

uv add simpleeval

poetry

poetry add simpleeval

Installing simpleeval

Before you install

No runtime dependencies and a single-file design keep installation friction minimal. The package is actively maintained with a recent release, and has been stable since its initial 2014 release.

License in practice

MIT license is permissive and imposes no restrictions on use, modification, or distribution in commercial or private projects.

Quickstart

from simpleeval import simple_eval

result = simple_eval("21 + 21")
print(result)  # 42

# With custom functions
result = simple_eval("square(11)", functions={"square": lambda x: x*x})
print(result)  # 121

Requires Python 3.9 or later.

Verify before relying

  • Whether the default MAX_POWER limit of 4000000 and MAX_STRING_LENGTH of 100000 characters are appropriate for your use case.
  • Whether the package's DOS protection against long-running or memory-intensive expressions is sufficient for your deployment context.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.9)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 151 days since the last release
Last repo commit
First released
Downloads 15,002,011/month — #1,206 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: simpleeval-1.0.7-py3-none-any.whl

Keywords: ast, eval, expression, parse, simple

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonTopic :: Software Development :: Libraries :: Python Modules

Tags

safe expression evaluationformula parser without evaluser-defined expressionsmath expression evaluatorsandboxed expression enginesimple formula calculatorast-based expression parser
expression-evaluationsandboxingformula-parser

More Python Modules packages