This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

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
Release History

Release History

1.2

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
optional_import-1.2.tar.gz (2.4 kB) Copy SHA256 Checksum SHA256 Source Jun 19, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting