Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Imports an object based on a string import_string('package.module:function_name')() - Based on werkzeug.utils

License

Notifications You must be signed in to change notification settings

rochacbruno-archive/import_string

Repository files navigation

DEPRECATED don't use it. Please do:

import importlib
foopath = 'src.apis.foo.Foo'

module_name = '.'.join(foopath.split('.')[:-1]) # to get src.apis.foo
foo_module = importlib.import_module(module_name)
clazz_name = foopath.split('.')[-1] # to get Foo
Foo = getattr(module_name, clazz_name)
print Foo()

import_string

Documentation Status Updates

Imports an object based on a string

Features

Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (.) or with a colon as object delimiter (:). If silent is True the return value will be None if the import fails.

Usage

import import_string

module = import_string('my_system.my_package.my_module')

function = import_string('my_system.my_module:some_function')

Class = import_string('my_system.my_module:SomeClass', silent=True)
# If path doesn't exist Class = None

Live demo

See it in action here: https://repl.it/EGdS/0

Credits

About

Imports an object based on a string import_string('package.module:function_name')() - Based on werkzeug.utils

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published