Import something if it exists.
Project description
Usage
This library contains only the context manager optional_import:
from optional_import import optional_import
An import within the optional_import context fails silently if the import does not exist.
Example: Optionally importing a module foo
Import foo is it exists; do nothing otherwise.
with optional_import():
import foo
Example: Optionally importing local settings from Django settings.py:
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 as:
try:
import foo
except ImportError:
pass
But this approach is deficient: If foo exists but raises ImportError, we want that error to be raised, but instead it is unintentionally swallowed by the except clause.
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
optional_import-1.0.tar.gz
(2.1 kB
view hashes)