Registry / serialization / asteval

asteval

JSON →
library1.0.10pypypi✓ verified 24d ago

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.

pip install asteval
INSTALL
IMPORT
SIG · ASTEVAL
A
asteval
serializationpythonv1.0.10
Install
1.5s avg
Import
43ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.10 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.046s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Interpreter
from asteval import Interpreter
from asteval.asteval import AstEval
`Interpreter` is the primary and recommended class for creating an AST evaluator instance, often aliased from `AstEval`.

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}")
Debug
Known issues
breakingFor enhanced security, `asteval` (starting from v1.0.1 and further hardened in v1.0.6 and v1.0.8) explicitly disallows or removes by default access to several potentially unsafe functions or modules. This includes `string.format()`, `numpy.memmap`, `numpy.linalg`, `numpy.polynomial`, and `numpy.fft`. Attempts to use these will raise errors unless they are explicitly and carefully re-enabled.
fix
Review your code for usage of these functions. If absolutely necessary, re-enable them by adding them to the `Interpreter`'s symbol table with extreme caution, and fully understand the security implications of doing so.
affects: >=1.0.1
gotchaLambda expressions (`lambda`) were not fully supported in `asteval` until version 1.0.8. Code relying on `lambda` will fail to parse or execute correctly on earlier `asteval` versions.
fix
Update `asteval` to version 1.0.8 or later to utilize `lambda` expressions. For older versions, refactor `lambda` functions into standard named `def` functions if possible.
affects: <1.0.8
gotchaThe behavior of `return` statements within `while`, `for`, or `with` blocks inside `asteval` functions (referred to as 'Procedures') was inconsistent in versions prior to 1.0.7. This could lead to `return` statements not correctly exiting the function as expected, potentially resulting in unexpected code execution.
fix
Update `asteval` to version 1.0.7 or later to ensure correct and predictable `return` behavior within loops and blocks inside `asteval` functions.
affects: <1.0.7
breaking`asteval` has progressively dropped support for older Python versions. As of version 1.0.7, Python 3.9 is no longer supported, and version 1.0.6 dropped support for Python 3.8. The current minimum required Python version is 3.10.
fix
Ensure your development and deployment environments use Python 3.10 or newer when working with recent versions of `asteval`.
affects: Python <3.10
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'importlib.metadata'
This error occurs when using asteval version 0.9.26 with Python 3.7, as the 'importlib.metadata' module is not available in Python versions prior to 3.8.
fix
Upgrade Python to version 3.8 or higher, or use a compatible version of asteval with your current Python version.
ImportError: cannot import name 'get_ast_names' from 'asteval'
This error arises when the 'get_ast_names' function is not available in the installed version of asteval, possibly due to version incompatibility.
fix
Ensure that both lmfit and asteval are updated to compatible versions that include the 'get_ast_names' function.
AttributeError: 'Parameters' object has no attribute '_asteval'
This error occurs when using lmfit version 0.9.15 with Python 2.7, where the 'Parameters' object lacks the '_asteval' attribute due to compatibility issues.
fix
Upgrade to a newer version of lmfit that supports Python 2.7, or preferably, upgrade to Python 3.x and use the latest versions of both lmfit and asteval.
ImportError: No module named six
This error happens when the 'six' module, a dependency of asteval, is not installed in the environment.
fix
Install the 'six' module using 'pip install six' before installing asteval.
NameError: name 'some_undefined_variable' is not defined
The variable or function you are trying to use within the `asteval` interpreter has not been defined or added to its symbol table.
fix
Initialize the variable or add the function to the `asteval` interpreter's symbol table (symtable) before evaluation.
Upgrade
Version history
1.0.10latest on PyPI · released Aug 21, 2026
Audit
Dependencies
numpyoptionalRequired for certain advanced mathematical functions, though many core NumPy functions are explicitly excluded by default for security. It is an optional dependency.
Agent activity
32 hits · last 30 days
node
26
Amazon
1
OpenAI (training)
1
Resources
asteval — pip install asteval · libregistry