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
Release history Release notifications | RSS feed
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
File details
Details for the file dynamic_imports-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: dynamic_imports-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3930b9112eb5d103610bd2272e3c1666143e4d8c7da623a8f641425c4cc1bcb2 |
|
MD5 | 3357120c953fcf74c6cfcff5dc5f03f0 |
|
BLAKE2b-256 | 58275fa5301081ba8f9600fcf09cea787395ca8a02eeeb8ced8f247bb47eda30 |