Skip to main content

env-exec

PyPI - Version PyPI - Python Version write-the - docs write-the - test codecov

Overview

The env-exec library is a versatile Python utility designed for managing computational environments and containers, as well as executing code within them. It aims to simplify the process of setting up, utilizing, and tearing down different kinds of environments, including but not limited to Conda environments.

Installation

pip install env-exec

Usage as a libary

Create an ephemeral Conda environment with dependencies installed.

with CondaEnv(dependencies=['python']) as env:
    # Execute a command in the environment
    env.exec('python -V') 
# The environment is automatically cleaned up after exiting the context manager

Capture the output of executed commands.

with CondaEnv(dependencies=['python']) as env:
    output = env.exec('python -V', capture_output=True)
    print(output.stdout) # Python 3.8.5

Give the environment a name to prevent it from being deleted. If the environment already exists, it will be reused.

with CondaEnv('my-env', dependencies=['python']) as env:
    env.exec('python -V')

CondaEnv('my-env').exists # True

Error if dependencies are missing from env.

# Raises MissingDependencyError
with CondaEnv('my-env', dependencies=['python', 'numpy']) as env:
    pass

Install dependencies if they are missing.

with CondaEnv('my-env', dependencies=['python', 'numpy'], install_missing=True) as env:
    env.exec('python -c "import numpy"')

Usage as a CLI

$ env-exec -d python=3.12.0 mamba python -V
Python 3.12.0
usage: env-exec [-h] [-n NAME] [-d DEPENDENCY] [-c CHANNEL] [-v] {conda,mamba,docker} ...

cli for executing commands in a virtual environment

positional arguments:
  {conda,mamba,docker}  The package manager to use.
  command               The command to execute.

options:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  The name of the environment.
  -d DEPENDENCY, --dependency DEPENDENCY
                        The dependencies to install.
  -c CHANNEL, --channel CHANNEL
                        Channel to use.
  -v, --verbose         If True, the output of the commands will be captured.

Features

Environment Management

  • Automated Setup: Create new computational environments programmatically.
  • Dependency Management: Specify and manage dependencies for each environment.
  • Dynamic Names: Generate random environment names or specify them manually.

Command Execution

  • Run Code: Execute code blocks, shell commands, or scripts within the environment.
  • Capture Output: Option to capture the output of executed commands.

Error Handling

  • Custom Exceptions: Well-defined exceptions for handling environment-specific issues.

Extensible Design

  • Pluggable Backends: Easily extend the library to support other environment managers or container systems.

Cleanup

  • Context Manager: Use as a context manager to ensure proper resource cleanup.

Dependencies

  • Python 3.7 or higher
  • Conda installed and accessible via the command line (if using CondaEnv)

Extending for Other Managers

You can also extend the library to create your own environment managers. Just inherit from the Env class and implement the required methods.

Error Handling

The library raises custom exceptions for more explicit error handling. These include:

  • ExecError: Raised if an error occurs while executing a command in the environment.
  • MissingDependencyError: Raised if missing dependencies are found and not installed.

Contributing

Contributions are welcome! If you have a feature request, bug report, or wish to contribute code, please feel free to open an issue or a pull request.

License

The code in this project is licensed under MIT. Please see the accompanying LICENSE file for details.

Metadata

Release files for env-exec 1.6.0

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

Source distribution (sdist)

Source distribution for env-exec 1.6.0
File Size Uploaded Provenance
env_exec-1.6.0.tar.gz 11.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for env-exec 1.6.0
File Interpreter ABI Platform
env_exec-1.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / env_exec-1.6.0.tar.gz

Download URL env_exec-1.6.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9b332e34cdc104a138199fff401f8ebed79282861c426a2dba5bc24095083cbf
BLAKE2b-256 checksum
How to use checksums
466766db94b8d59f9e182e88267a2e5c6558e015e918584dde136f26d58d8c26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.0

Release files / env_exec-1.6.0-py3-none-any.whl

Download URL env_exec-1.6.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b64d87bba5b884601e779f6d8ccb1dee719e70730fd4e49653de479db711e0b
BLAKE2b-256 checksum
How to use checksums
d96c16a4668482cbe8d33cc1a2c5cdd9d3afa163782cff5cc57472054abeb5dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.0

Security for env-exec 1.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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