Skip to main content

Print a hint when one of optional dependencies of a package not found

Project description

Optional Dependency Hint

This package gives a clearer ImportError message when an optional dependency is missing, including an install hint like pip install mypkg[extra].

Installation

pip install optional-dependency-hint

Requirements

  • Python >=3.8

Quick Start

import optdep

# Declare `tqdm` as an optional dependency of `mypkg`
# and tell users how to install it.
optdep.add("tqdm", "mypkg", "progbar")

import tqdm
# If `tqdm` is missing:
# ImportError: Optional dependency tqdm is not installed.
# You may install it with mypkg by: `pip install mypkg[progbar]`

If you only want the default message without install instructions:

import optdep

optdep.add("tqdm")

import tqdm
# If `tqdm` is missing:
# ImportError: Optional dependency tqdm is not installed.

API

optdep.add(module_name, this_pkg_name=None, extra=None, exist_behavior="skip")

  • module_name (str): target optional dependency module name.
  • this_pkg_name (str | None): your package name shown in hint message.
  • extra (str | None): extra name shown as pkg[extra].
  • exist_behavior ("skip" | "overwrite" | "error"): behavior when the module was already registered.

exist_behavior semantics

  • skip (default): keep old registration.
  • overwrite: replace old registration with the new one.
  • error: raise RuntimeError.

Notes

  • Call optdep.add(...) before importing the optional module.
  • This package registers an import hook via sys.meta_path at import time.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optional_dependency_hint-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optional_dependency_hint-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file optional_dependency_hint-0.1.0.tar.gz.

File metadata

File hashes

Hashes for optional_dependency_hint-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8e44674cf5529ba6c99f3d8d40cf0d0a28a64a822aab872a19e611a5873feedf
MD5 722001c7fcfdc5221e109bed257b2c83
BLAKE2b-256 1f36a9d47e8fdd1be4298d54d5f251595fafc2fa850549ac9ffe9ce0569128f9

See more details on using hashes here.

File details

Details for the file optional_dependency_hint-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for optional_dependency_hint-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0fd85e57853558c62019d8d9f103dcae323dc25b2cdafc6e356428cd324e2b7
MD5 2ec1e92b1442577911c9e4bf0fbcb91c
BLAKE2b-256 f58a3067f73a5c049e2e7ac085e6c9b2fb4a6574311b853702a3ed54a4239eec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page