Skip to main content

Programmatically execute commands in environments.

Project description

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.

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

env_exec-1.6.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

env_exec-1.6.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file env_exec-1.6.0.tar.gz.

File metadata

  • Download URL: env_exec-1.6.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for env_exec-1.6.0.tar.gz
Algorithm Hash digest
SHA256 9b332e34cdc104a138199fff401f8ebed79282861c426a2dba5bc24095083cbf
MD5 b190fb7c9163e852ee7294314966f5d6
BLAKE2b-256 466766db94b8d59f9e182e88267a2e5c6558e015e918584dde136f26d58d8c26

See more details on using hashes here.

File details

Details for the file env_exec-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: env_exec-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for env_exec-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b64d87bba5b884601e779f6d8ccb1dee719e70730fd4e49653de479db711e0b
MD5 a515879441c2c7bc92e972303f8a96e3
BLAKE2b-256 d96c16a4668482cbe8d33cc1a2c5cdd9d3afa163782cff5cc57472054abeb5dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page