Skip to main content

🐍 pytherpreter 🐍

A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.

This repository contains the Python interpreter tool extracted from HuggingFace’s smolagents project. Big hug to the HuggingFace team for their initial implementation! 🤗

Some improvements over the smolagents tool:

  • Supports async code execution using the async_evaluate function and AsyncPythonInterpreter class.
  • Improved function call resolution.
  • Supports custom subscriptable objects.
  • No external dependencies.
  • More flexible print handling.

Installation

pip install pytherpreter

Latest development version:

pip install git+ssh://git@github.com/aremeis/pytherpreter.git

or

pip install git+https://github.com/aremeis/pytherpreter.git

Usage

Using evaluate

This function evaluates Python code and returns the result.

from pytherpreter import evaluate

result = evaluate("""
from math import sqrt
sqrt(4)
""")
print(result)

# Output:
# 2.0

The evaluate function returns the result of the last expression in the code.

Using PythonInterpreter

This class is a wrapper around the evaluate function that keeps the state of the interpreter between calls. Variables and functions defined by the code will be be available in subsequent calls.

from pytherpreter import PythonInterpreter

interpreter = PythonInterpreter()
result = interpreter("x = 3")
print(result)

# Output:
# 3

result = interpreter("x += 1")
print(result)

# Output:
# 4

Printing

You may provide a stdout argument to capture the output of print statements in the code.

from pytherpreter import evaluate
import io
stdout = io.StringIO()
result = evaluate("print('Hello, World!')", stdout=stdout)
print(stdout.getvalue())

# Output:
# Hello, World!

Variables

You may provide a variables argument to preset variables and capture changes to them.

from pytherpreter import evaluate
variables = {"x": 3}
result = evaluate("x += 1", variables=variables)
print(variables["x"])

# Output:
# 4

Safeguards

Built-in functions

You may provide a builtin_functions argument containing a dictionary of built-in functions the code is allowed to call. If you don't provide this argument, the code will only be able to call the built-in functions in BASE_BUILTIN_FUNCTIONS.

The code will not be able to modify the provided built-in functions.

Modules

By default, the code will only be able to import the modules in BASE_BUILTIN_MODULES. You may provide an authorized_imports argument to allow the code to import additional modules.

Documentation

For more details, the reference documentation is available here.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Metadata

Release files for pytherpreter 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytherpreter 0.2.3
File Size Uploaded
pytherpreter-0.2.3.tar.gz 47.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytherpreter 0.2.3
File Interpreter ABI Platform
pytherpreter-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 74.9 kB

Release files / pytherpreter-0.2.3.tar.gz

Download URL pytherpreter-0.2.3.tar.gz
Size 47.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b0dded7eb075cc23c7a907a249b457e8b628c8130b140e2514686e9b22e0c166
BLAKE2b-256 checksum
How to use checksums
7dc6b015f949f3c61d6d17b5e1755dd759952027eda3fe21c4c456dc1face585
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.3

Release files / pytherpreter-0.2.3-py3-none-any.whl

Download URL pytherpreter-0.2.3-py3-none-any.whl
Size 27.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f05e0df3ce6ffcddc96b735fcacf064a7ce2fb0aeaee5ba502d0ea7b7065301
BLAKE2b-256 checksum
How to use checksums
ad106b3f964467cd3feb3d159da9bcd62744449ca63b302605b09b817b4699b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page