Skip to content

Instantly share code, notes, and snippets.

@theskumar
Created September 3, 2014 06:52
Show Gist options
  • Select an option

  • Save theskumar/1af29df692a911a688c9 to your computer and use it in GitHub Desktop.

Select an option

Save theskumar/1af29df692a911a688c9 to your computer and use it in GitHub Desktop.

Revisions

  1. theskumar created this gist Sep 3, 2014.
    15 changes: 15 additions & 0 deletions import_from_string.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    def import_from_string(val):
    """
    Attempt to import a class from a string representation.
    From: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/settings.py
    """
    try:
    # Nod to tastypie's use of importlib.
    parts = val.split('.')
    module_path, class_name = '.'.join(parts[:-1]), parts[-1]
    module = importlib.import_module(module_path)
    return getattr(module, class_name)
    except ImportError as e:
    msg = "Could not import '%s' for setting. %s: %s." % (val, e.__class__.__name__, e)
    raise ImportError(msg)