From 33b12d3c18a35f85785227267347f9ac7f7273a4 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 14 Jan 2013 12:54:43 -0500 Subject: Implement importutils.try_import. Adds a new (simple) importutils.try_import function which can return a module name or a default (by default this is None). This should help clean up some of our try: import foo except... blocks in modules. This commit also drops the dependency on python-extras which is no longer needed. Fixes LP Bug #1099501. Change-Id: I8591f79983bdad67a50d1aaae6dce8428dfce084 --- openstack/common/importutils.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'openstack/common/importutils.py') diff --git a/openstack/common/importutils.py b/openstack/common/importutils.py index 2a28b45..9dec764 100644 --- a/openstack/common/importutils.py +++ b/openstack/common/importutils.py @@ -57,3 +57,11 @@ def import_module(import_str): """Import a module.""" __import__(import_str) return sys.modules[import_str] + + +def try_import(import_str, default=None): + """Try to import a module and if it fails return default.""" + try: + return import_module(import_str) + except ImportError: + return default -- cgit