Metadata-Version: 2.1
Name: pydepinject
Version: 0.0.2.dev0
Summary: A package to dynamically inject requirements into a virtual environment.
Author: pydepinject
License: MIT
Project-URL: homepage, https://github.com/pydepinject/pydepinject
Project-URL: documentation, https://github.com/pydepinject/pydepinject
Project-URL: repository, https://github.com/pydepinject/pydepinject
Keywords: virtualenv,requirements,dependency management
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: packaging>=22.0
Requires-Dist: typing_extensions
Provides-Extra: lint
Requires-Dist: ruff==0.4.7; extra == "lint"
Requires-Dist: pyright==1.1.365; extra == "lint"
Requires-Dist: isort==5.13.2; extra == "lint"
Provides-Extra: test
Requires-Dist: pytest==8.2.1; extra == "test"
Requires-Dist: pytest-cov==5.0.0; extra == "test"
Requires-Dist: pytest-xdist==3.6.1; extra == "test"
Provides-Extra: build
Requires-Dist: check-manifest==0.49; extra == "build"
Requires-Dist: build==1.2.1; extra == "build"
Requires-Dist: wheel==0.43.0; extra == "build"
Requires-Dist: setuptools==70.0.0; extra == "build"

# 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:

```python
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:

```python
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:

```python
@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:

```python
@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:

```python
@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:

```bash
pytest
```

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
