Necessary
Python package that can be used to enforce optional dependencies are installed when a module is imported. Necessary stands for "Now Ensures Correct Existence of Select Software, or Add Resource Yourself!"
Necessary is available on PyPI, and can be installed with the following command:
pip install necessary
How to Use
As Context Manager
Simply use necessary.necessary to get a context manager import a module.
from necessary import necessary
with necessary('torch'):
# this will raise a nicely written error if
# torch is not installed
import torch
If you want to just soft fail when a necessary module is not available (that is, have necessary return False), use soft:
try:
# assuming torch is not installed
necessary('torch')
except ImportError:
out = necessary('torch', soft=True)
assert out is False
If you want to request a minimum version, use Python's requirements syntax:
with necessary('torch>=1.12.0'):
# this will raise an error if
# torch is not installed or if
# the installed version is less than 0.12.0
import torch
You can also check multiple packages in the same necessary call, or combine multiple requirements:
with necessary(['torch>=1.12.0', 'numpy>=1.20,<1.25']):
# this will raise an error if torch >= 1.12.0 or numpy are not installed
import torch
import numpy
Finally, we can customize the message that is raised if a necessary module is not installed; use {module_name} and {module_version} to insert the module name and version respectively.
with necessary('torch', message='I am missing {module_name}/{module_version}'):
import torch
As Function or Class Decorator
You can also use necessary as a function or class decorator:
from necessary import Necessary
# decorating a function
@Necessary('torch')
def my_function():
import torch
# decorating a class
@Necessary('torch')
class MyClass:
def __init__(self):
import torch
All of the same functionality is available in the decorator form as in the context manager form.
Metadata
Release files for necessary 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| necessary-0.4.3.tar.gz | 11.0 kB | Without provenance | Details for necessary-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| necessary-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for necessary-0.4.3-py3-none-any.whl |
Total release size: 21.3 kB
Release files / necessary-0.4.3.tar.gz
| Download URL | necessary-0.4.3.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
381923f9efb13ed613e4a094f154ce3ce36935a39bed58017cc6004be1f74a86
|
|
BLAKE2b-256 checksum How to use checksums |
ccd501b84f230f7d08d3aebd82937ffefd279b0ca8ab2d03cf3f41e8c6222e25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Release files / necessary-0.4.3-py3-none-any.whl
| Download URL | necessary-0.4.3-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bfb4cd5396928c0affb4f4d4963c1eb7502cd58106b98692e3a3ccd588bd142c
|
|
BLAKE2b-256 checksum How to use checksums |
d2fd7b5a09553f81e7ff5bae6af151d0f61bcb3dcff118d670ae57e728ade521
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Security for necessary 0.4.3
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.