https://github.com/thesis/optional_import
Optional imports in Python
https://github.com/thesis/optional_import
Last synced: 2 months ago
JSON representation
Optional imports in Python
- Host: GitHub
- URL: https://github.com/thesis/optional_import
- Owner: thesis
- License: mit
- Created: 2014-06-17T22:39:05.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-06-19T03:39:24.000Z (about 12 years ago)
- Last Synced: 2026-01-13T17:25:18.874Z (6 months ago)
- Language: Python
- Size: 234 KB
- Stars: 1
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
.. figure:: https://circleci.com/gh/cardforcoin/optional_import.png?circle-token=d834124e03717f6619b867f13c8a85f254298df5
:alt: Build status
optional_import
===============
Optional imports in Python
.. pypi - Everything below this line goes into the description for PyPI.
Usage
-----
This library contains only the context manager ``optional_import``:
.. code:: python
>>> from optional_import import optional_import
A successful import works as usual:
.. code:: python
>>> with optional_import():
... import collections
>>> type(collections)
If the import does not exist, ``optional_import`` suppresses the
``ImportError`` that would otherwise be raised.
.. code:: python
>>> 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.
.. code:: python
with optional_import():
from .settings_local import *
Why not just catch ``ImportError``?
-----------------------------------
Optional imports can almost be achieved simply by catching ``ImportError``:
.. code:: python
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``:
.. code:: python
>>> with optional_import():
... import bad
Traceback (most recent call last):
...
ImportError: No module named unicorns