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.0.tar.gz
(7.7 kB
view hashes)
Built Distribution
Close
Hashes for necessary-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53ccbee2a2f69c07d58a155fe18337c36c59e9a8009edaf7d6f652e5d1fde984 |
|
MD5 | e514d17a6496420267028cfdc48a5ca5 |
|
BLAKE2b-256 | b5737b828ae86f2bcffab9ae0b4e0be3d99eb0f7b9e926129162f7d4bd96447c |