Skip to main content

A package to dynamically inject requirements into a virtual environment.

Project description

Requirement Manager

This project provides a RequirementManager (requires is an alias) class to manage Python package requirements using virtual environments. It can be used as a decorator or context manager to ensure specific packages are installed and available during the execution of a function or code block.

Features

  • Automatically creates and manages virtual environments.
  • Checks if the required packages are already installed.
  • Installs packages if they are not already available.
  • Supports ephemeral virtual environments that are deleted after use.
  • Can be used as a decorator or context manager.

Installation

pip install pydepinject

Usage

Decorator

To use the requires as a decorator, simply decorate your function with the required packages:

from pydepinject import requires


@requires("requests", "numpy")
def my_function():
    import requests
    import numpy as np
    print(requests.__version__)
    print(np.__version__)

my_function()

Context Manager

You can also use the requires as a context manager:

from pydepinject import requires


with requires("requests", "numpy"):
    import requests
    import numpy as np
    print(requests.__version__)
    print(np.__version__)

Virtual Environment with specific name

The requires can create a virtual environment with a specific name:

@requires("requests", venv_name="myenv")
def my_function():
    import requests
    print(requests.__version__)


with requires("pylint", venv_name="myenv"):
    import pylint
    print(pylint.__version__)
    import requests  # This is also available here because it was installed in the same virtual environment
    print(requests.__version__)


# The virtual environment name can also be set as PYDEPINJECT_VENV_NAME environment variable
import os
os.environ["PYDEPINJECT_VENV_NAME"] = "myenv"

@requires("requests")
def my_function():
    import requests
    print(requests.__version__)


with requires("pylint"):
    import pylint
    print(pylint.__version__)
    import requests  # This is also available here because it was installed in the same virtual environment
    print(requests.__version__)

Reusable Virtual Environments

The requires can create named virtual environments and reuse them across multiple functions or code blocks:

@requires("requests", venv_name="myenv", ephemeral=False)
def my_function():
    import requests
    print(requests.__version__)


with requires("pylint", venv_name="myenv", ephemeral=False):
    import pylint
    print(pylint.__version__)
    import requests  # This is also available here because it was installed in the same virtual environment
    print(requests.__version__)

Managing Virtual Environments

The requires can automatically delete ephemeral virtual environments after use. This is useful when you want to ensure that the virtual environment is clean and does not persist after the function or code block completes:

@requires("requests", venv_name="myenv", ephemeral=True)
def my_function():
    import requests
    print(requests.__version__)

my_function()

Logging

The requires uses the logging module to provide debug information. By default, it logs to the console at the DEBUG level. You can adjust the logging configuration as needed.

Unit Tests

Unit tests are provided to verify the functionality of the requires. The tests use pytest and cover various scenarios including decorator usage, context manager usage, ephemeral environments, and more.

Running Tests

To run the unit tests, ensure you have pytest installed, and then execute the following command:

pytest

License

This project is licensed under the MIT License. See the LICENSE file for more 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

pydepinject-0.0.1.dev1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

pydepinject-0.0.1.dev1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pydepinject-0.0.1.dev1.tar.gz.

File metadata

  • Download URL: pydepinject-0.0.1.dev1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.14

File hashes

Hashes for pydepinject-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 ab72119fae5ffdf3ad9d2afefc5102cac28714e485b2ca6764b609dd0f5fb09b
MD5 32bf6941c30896ac7d786eaa40b07ff8
BLAKE2b-256 a1fa47c02e8154b68a5299ce391dc8f2c53ec3bd553324695d10bf275dc96b7c

See more details on using hashes here.

File details

Details for the file pydepinject-0.0.1.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydepinject-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 29bff9e8a711892731b8c845b7e88d255da1410d983473ac4d9668720e2fdc0e
MD5 adc6f858b223f5814bb6668d746af939
BLAKE2b-256 31b81b2cca8aefc3659ab0f3b758d5d5943a1aaeba816b20bda64e187f00378b

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