Python package to enforce optional dependencies
Project description
Necessary
Python package to enforce optional dependencies.
Install with:
pip install necessary
How to Use
Simply use necessary.necessary
to conditionally import a module.
from necessary import necessary
if necessary('torch'):
# this will raise an 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_check
:
try:
# assuming torch is not installed
necessary('torch')
except ImportError:
out = necessary('torch', soft_check=True)
print(out) # this prints False
If you want to request a minimum version, use min_version
:
if 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::
if necessary([('torch', '1.12.0'), 'numpy']):
# 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.
if necessary('torch', message='I am missing {module_name}/{module_version}'):
import torch
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
necessary-0.2.1.tar.gz
(7.9 kB
view hashes)
Built Distribution
Close
Hashes for necessary-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 854b8e3831f0b089718903cdb0df3274865358caca29f99404d30ce194cc42df |
|
MD5 | 343319c7b81a21c90cc75012a74f3e01 |
|
BLAKE2b-256 | 1cb541833249309a857357ea0bbbfae506715200b19d9c30a372f4b46cf02bd8 |