Skip to main content

Usage

This library contains only the context manager optional_import:

>>> from optional_import import optional_import

A successful import works as usual:

>>> with optional_import():
...     import collections
>>> type(collections)
<type 'module'>

If the import does not exist, optional_import suppresses the ImportError that would otherwise be raised.

>>> import unicorns
Traceback (most recent call last):
  ...
ImportError: No module named unicorns

>>> with optional_import():
...     import unicorns

>>> unicorns
Traceback (most recent call last):
  ...
NameError: name 'unicorns' is not defined

Example: Django local settings

A common pattern in Django is to put default settings in settings.py, put optional site-specific settings in settings_local.py, and import * from the local settings file if it exists.

with optional_import():
    from .settings_local import *

Why not just catch ImportError?

Optional imports can almost be achieved simply by catching ImportError:

try:
    import foo
except ImportError:
    pass

But this approach introduces a problem: If foo exists but raises ImportError, we want that error to be raised, but instead it is swallowed by the except clause.

With optional_import, the error is raised as desired. In the following example, the bad module tries to import a nonexistent package unicorns:

>>> with optional_import():
...     import bad
Traceback (most recent call last):
  ...
ImportError: No module named unicorns

Metadata

Release files for optional_import 1.2

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

Source distribution (sdist)

Source distribution for optional_import 1.2
File Size Uploaded Provenance Actions
optional_import-1.2.tar.gz 2.4 kB Without provenance Details for optional_import-1.2.tar.gz

Release files / optional_import-1.2.tar.gz

Download URL optional_import-1.2.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cd651eb9824a93d586905a4c54c19a9f2865f59201b022dcd32a8fc71f1faf86
BLAKE2b-256 checksum
How to use checksums
dec6cbbda50cb62ade7d16acaaa5d1b1a34589281ee8b24d54ba36d5d6d35811
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for optional_import 1.2

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.

Release history Release notifications | RSS feed

This release

1.2 This release

1 release file 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