Skip to main content
Documentation Status https://github.com/MacHu-GWU/soft_deps-project/actions/workflows/main.yml/badge.svg https://codecov.io/gh/MacHu-GWU/soft_deps-project/branch/main/graph/badge.svg https://img.shields.io/pypi/v/soft-deps.svg https://img.shields.io/pypi/l/soft-deps.svg https://img.shields.io/pypi/pyversions/soft-deps.svg https://img.shields.io/badge/✍️_Release_History!--None.svg?style=social&logo=github https://img.shields.io/badge/⭐_Star_me_on_GitHub!--None.svg?style=social&logo=github
https://img.shields.io/badge/Link-API-blue.svg https://img.shields.io/badge/Link-Install-blue.svg https://img.shields.io/badge/Link-GitHub-blue.svg https://img.shields.io/badge/Link-Submit_Issue-blue.svg https://img.shields.io/badge/Link-Request_Feature-blue.svg https://img.shields.io/badge/Link-Download-blue.svg

Welcome to soft_deps Documentation

https://soft-deps.readthedocs.io/en/latest/_static/soft_deps-logo.png

soft-deps is a Python pattern for elegant optional dependency management that prioritizes developer experience over lazy loading. Unlike true lazy imports, soft-deps immediately attempts to import dependencies at module load time - if the dependency is installed, you get the real module with full functionality. If it’s missing, you get a helpful proxy object that provides complete IDE type hints and auto-completion, but raises informative error messages only when you actually try to use the missing functionality.

This approach gives you the best of both worlds: seamless development experience with full IDE support when dependencies are available, and graceful degradation with clear installation guidance when they’re not, all while maintaining zero code invasiveness in your implementation.

Quick Start

Why use soft-deps?

  • For library authors: Provide optional features without forcing users to install heavy dependencies

  • For users: Get full IDE support and autocomplete even for optional dependencies

  • For teams: Graceful degradation with clear error messages instead of cryptic import failures

When to use soft-deps?

Use soft-deps when your library has optional features that depend on external packages:

  • Data science libraries with optional visualization (matplotlib, plotly)

  • Web frameworks with optional database drivers (psycopg2, pymongo)

  • CLI tools with optional cloud integrations (boto3, google-cloud)

  • Any library where you want some features to work without heavy dependencies

How it works:

Library authors use the soft-deps pattern in their code:

# your_library.py
from soft_deps.api import MissingDependency

# Try to import optional dependency
try:
    import pandas as pd
except ImportError:
    # Create helpful proxy if missing
    pd = MissingDependency("pandas", "pip install pandas")

def export_to_excel(data):
    """Export data to Excel file (requires pandas)."""
    # IDE gets full autocomplete here even if pandas not installed
    # Error only occurs when this line actually executes
    return pd.DataFrame(data).to_excel("output.xlsx")

Users get seamless experience regardless of what’s installed:

# user_script.py
from your_library import export_to_excel

# This imports successfully even without pandas installed
# IDE provides full autocomplete and type hints

try:
    export_to_excel({"col1": [1, 2, 3]})
except ImportError as e:
    print(e)  # "To use `pandas.DataFrame`, pip install pandas."

The difference:

# ❌ Traditional approach - breaks immediately
import pandas as pd  # ImportError if not installed

# ❌ Lazy imports - no IDE support
def export_data():
    import pandas as pd  # IDE can't help with autocomplete

# ✅ soft-deps - best of both worlds
try:
    import pandas as pd
except ImportError:
    pd = MissingDependency("pandas", "pip install pandas")
# IDE gets full support, graceful errors when actually used

What Raises and What Does Not

Only real use of the missing dependency raises an ImportError. Merely mentioning it does not, so type hints work with or without from __future__ import annotations:

Does not raise

Raises ImportError

pd.DataFrame, npt.NDArray[np.float64], pd.DataFrame | None (each returns another placeholder, so type hints and typing.get_type_hints() work)

calling: pd.DataFrame(data), pd.read_csv(path)

repr(pd), pd == other, hash(pd), pd in some_list, copy.copy(pd), pickle.dumps(pd), hasattr(pd, "__wrapped__")

str(pd), f"{pd}", for x in pd, len(pd), with pd:

if pd: (always true, like a real module)

<, >, +, -, *, /, isinstance(x, pd.DataFrame), class MyFrame(pd.DataFrame)

The error message names what was used, for example To use `pandas.DataFrame`, pip install your_library[excel].

Keep these out of module level, because they run while the module is imported:

  • decorators from the dependency, such as @numba.jit;

  • classes that subclass something from the dependency, such as class MyModel(pydantic.BaseModel);

  • module-level calls, such as session = boto3.Session().

Put such code in a module that only the optional feature imports.

Install

soft_deps is released on PyPI, so all you need is to:

$ pip install soft-deps

To upgrade to latest version:

$ pip install --upgrade soft-deps

Use with AI Coding Agents

This repo ships an Agent Skill that documents the public API of soft_deps, so an AI coding agent such as Claude Code can use the library without reading its source. It lives in .claude/skills/pypi-soft_deps. To use it in your project, copy it into your project’s .claude/skills/ folder. Run this from your project’s root:

$ git clone --depth 1 https://github.com/MacHu-GWU/soft_deps-project.git /tmp/soft_deps-project
$ mkdir -p .claude/skills
$ cp -R /tmp/soft_deps-project/.claude/skills/pypi-soft_deps .claude/skills/

Metadata

Release files for soft_deps 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for soft_deps 0.2.0
File Size Uploaded Provenance
soft_deps-0.2.0.tar.gz 14.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for soft_deps 0.2.0
File Interpreter ABI Platform
soft_deps-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / soft_deps-0.2.0.tar.gz

Download URL soft_deps-0.2.0.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
47bb85af1add2e0f05633351eb9eaea30c8a1d97e59cf666535d332542644c4f
BLAKE2b-256 checksum
How to use checksums
68dc45a77176200e8c4063da12a0a4ce35babdcd489e467285ccb1c39ecdb0af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / soft_deps-0.2.0-py3-none-any.whl

Download URL soft_deps-0.2.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0f875f405d3631a36f469b45d4ec84eef4a66c7ff82e7106bc688fcc72181eb
BLAKE2b-256 checksum
How to use checksums
1e697b2ec8f74f3c51c256c20b0b34469b442da47330b5bdcfde7e0d6fcd5785
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for soft_deps 0.2.0

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page