A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.
Project description
pytherpreter
A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.
This repository extracts the Python interpreter tool from HuggingFace’s smolagents project.
Installation
pip install pytherpreter
Usage
evaluate_python_code
evaluate_python_code is a function that evaluates Python code and returns the result.
from pytherpreter import evaluate_python_code
result, final_answer = evaluate_python_code("""
from math import sqrt
sqrt(4)
""")
print(result)
print(final_answer)
# Output:
# 2.0
# False
By default, the evaluate_python_code function will return the result of the last expression in the code.
However, you can also return a value from the code by using the final_answer function:
result, _ = evaluate_python_code("""
from math import sqrt
final_answer(sqrt(4))
""")
print(result)
print(result)
print(final_answer)
# Output:
# 2.0
# True
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytherpreter-0.0.1.tar.gz.
File metadata
- Download URL: pytherpreter-0.0.1.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62a4d79cd0d4819899229369c2473c4228aed58a1dfb6c4a7435d90cce911a32
|
|
| MD5 |
da9512498059b422318c54d556ae8192
|
|
| BLAKE2b-256 |
fae70deb8cade88482d0ec525ce774364eb5568352c81d7af22e33e031517b70
|
File details
Details for the file pytherpreter-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pytherpreter-0.0.1-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daf6a57979e1f8481b975184aecd1134ec464c879724b71cddf2d14007768e0a
|
|
| MD5 |
680827d749073d36e6cb645f0a5bb557
|
|
| BLAKE2b-256 |
a29e5f839e47af3f5a2ee34dd2700fb8a847608843c8cd63d9ccf353c63e4c22
|