
Security News
NIST Under Federal Audit for NVD Processing Backlog and Delays
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
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.
pip install pydepinject
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()
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__)
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__)
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__)
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()
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 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.
To run the unit tests, ensure you have pytest
installed, and then execute the following command:
pytest
This project is licensed under the MIT License. See the LICENSE file for more details.
FAQs
A package to dynamically inject requirements into a virtual environment.
We found that pydepinject demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.
Security News
TypeScript Native Previews offers a 10x faster Go-based compiler, now available on npm for public testing with early editor and language support.