Crates.io | py_env |
lib.rs | py_env |
version | 1.2.0 |
source | src |
created_at | 2024-02-20 10:33:43.457987 |
updated_at | 2024-04-04 20:38:59.3537 |
description | A wrapper for Python commands to easily install dependencies and run Python scripts in an optionally impersistent environment. |
homepage | |
repository | https://github.com/uptudev/py_env/ |
max_upload_size | |
id | 1146213 |
size | 13,130 |
A Rust library to run Python scripts and install dependencies within a given environment path.
Simply add the library as a dependency in your Cargo.toml as follows, and invoke via the usage instructions.
[dependencies]
py_env = "1.2.0"
This library uses a very simple syntax to run Python scripts. To create a Python environment, simply run PyEnv::at(PathBuf)
.
use py_env::PyEnv;
let env = PyEnv::at("./py_test");
use py_env::PyEnv;
PyEnv::at("./py_test")
.execute("print('hello world')")
.expect("Failed to execute code");
The following code installs numpy into the ./py_test
directory's site-packages and uses it in executed code.
use py_env::PyEnv;
PyEnv::at("./py_test")
.install("numpy")
.expect("Failed to install numpy")
.execute("a = numpy.arange(15).reshape(3, 5); print(a.shape)")
.expect("Failed to execute code");
The following code deletes the python environment off of the disk once it's done running.
use py_env::PyEnv;
PyEnv::at("./py_test")
.persistent(false)
.install("numpy").expect("Failed to install numpy");
try_
UnwrappersThe try_install()
and try_execute()
unwrapper functions panic upon errors being returned from their inner install()
and execute()
functions, which fail only if there is an error spawning the Python commands needed to run or upon waiting for them to finish running. Given that should only really happen if you don't have Python installed, we have provided these functions for convenience, but warn against their use in production code over handling the errors manually.
use py_env::PyEnv;
PyEnv::at("./py_test")
.persistent(false)
.try_install("numpy")
.try_execute("a = numpy.arange(15).reshape(3, 5); print(a.shape)");
PRs are always welcome and will be reviewed when I see them. Code is released under the MIT License (see below), and as such forking this repo to add features you'd like to see implemented would be greatly appreciated.
This code is licensed under the MIT License.