Skip to main content

Dynamically discover and import Python modules, classes, and functions.

Project description

Dynamically discover and import Python modules, classes, and functions.

Install

pip install dynamic_imports

Examples

Import a module via module name or file path

from dynamic_imports import import_module
module = import_module('my_package.my_module')
# or
module = import_module('/home/user/my_package/my_module.py')

Import a module attribute

from dynamic_imports import import_module_attr

function = import_module_attr('my_package.my_module', 'my_function')
# or
function = import_module_attr('/home/user/my_package/my_module.py', 'my_function')

Find all modules in a package or nested packages

from dynamic_imports import discover_modules

modules = discover_modules(
    package=my_package, # str `my_package' works too.
    search_subpackages=True,
    # return the actual module objects, not str names.
    names_only=False # this is the default value...
)

The search_in argument for all functions below can be an imported package or module, string name of the package (e.g. "my_package") or module (e.g. "my_package.my_module"), or path to a python file (e.g. "/home/user/my_package/my_module.py")

Find all implementations of a base class within a module.

from dynamic_imports import class_impls
from my_package.my_module import Base
from my_package import my_module

my_classes = class_impls(
    base_class=Base, # str 'Base' works too
    search_in=my_module,
    names_only=False # this is the default value...
)

Find all implementations of a base class within nested packages.

from dynamic_imports import class_impls
from my_package.my_module import Base
import my_package

my_classes = class_impls(
    base_class=Base, # str 'Base' works too.
    search_in=my_package
    search_subpackages=True,
    names_only=False # this is the default value...
)

Find all instances of a class within a module.

from dynamic_imports import class_inst
from my_package import my_module
from my_package.my_module import MyClass

my_classes_instances = class_inst(
    search_in=my_module,
    class_type=MyClass
)

Find all instances of a class within nested packages.

from dynamic_imports import class_inst
from my_package.my_module import MyClass
import my_package

my_classes_instances = class_inst(
    class_type=MyClass,
    search_in=my_package,
    search_subpackages=True,
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dynamic_imports-1.0.2-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page