No project description provided
Project description
Dynamic Imports
A Python library for dynamically discovering and importing modules, classes, and functions at runtime.
Features
- Dynamic Module Import: Import modules by name or file path
- Attribute Access: Import specific attributes (functions, classes) from modules
- Module Discovery: Find all modules within packages and subpackages
- Class Discovery: Find all subclasses of a base class across modules
- Instance Discovery: Find all instances of a class type within modules
- Flexible Input: Works with module objects, module names, and file paths
- Caching: Built-in caching for improved performance
Installation
pip install dynamic-imports
Quick Start
Import a module dynamically
from dynamic_imports import import_module
# Import by module name
module = import_module('my_package.my_module')
# Import by file path
module = import_module('/path/to/my_module.py')
Import a specific attribute from a module
from dynamic_imports import import_module_attr
# Get a function from a module
my_function = import_module_attr('my_package.my_module', 'my_function')
# Get a class from a module file
MyClass = import_module_attr('/path/to/my_module.py', 'MyClass')
Advanced Usage
Discover all modules in a package
from dynamic_imports import find_modules
import my_package
# Get all modules as objects (default)
modules = find_modules(
package=my_package, # Can also use string 'my_package'
search_subpackages=True, # Include nested packages
names_only=False # Return module objects (default)
)
# Get only module names
module_names = find_modules(
package='my_package',
search_subpackages=True,
names_only=True # Return strings instead of module objects
)
Find all instances of a class
from dynamic_imports import find_instances
from my_package.my_module import MyClass
import my_package
# Find instances in a specific module
instances = find_instances(
class_type=MyClass,
search_in='my_package', # or my_package
search_subpackages=False
)
# Find instances across all subpackages
all_instances = find_instances(
class_type=MyClass,
search_in=my_package, # or 'my_package'
search_subpackages=True
)
Find all subclasses of a base class
from dynamic_imports import find_subclasses
from my_package.base import BaseClass
import my_package
# Find subclasses in a specific module
subclasses = find_subclasses(
base_class=BaseClass, # Can also use string 'BaseClass'
search_in=my_package,
search_subpackages=False,
)
# Find subclasses across all subpackages
all_subclasses = find_subclasses(
base_class=BaseClass,
search_in=my_package, # Search entire package
search_subpackages=True
)
Import-Free Discovery with names_only=True
When using the discovery functions (find_modules and find_subclasses) with names_only=True, the library leverages Python's built-in pyclbr (Python class browser) module to perform static analysis of Python source files without executing them, avoiding performance overhead of actual imports.
from dynamic_imports import find_subclasses
plugin_names = find_subclasses(
base_class='BasePlugin',
search_in='plugins',
search_subpackages=True,
names_only=True # Uses pyclbr for static analysis
)
API Reference
Core Functions
import_module(name_or_path: Union[Path, str]) -> ModuleType
Import a module by its name or file path.
- Parameters:
name_or_path: Module name (e.g.,'os.path') or file path (e.g.,'/path/to/module.py')
- Returns: The imported module object
- Note: Results are cached using LRU cache for performance
import_module_attr(module_name_or_path: Union[Path, str], attr_name: str) -> Any
Import a specific attribute from a module.
- Parameters:
module_name_or_path: Module name or file pathattr_name: Name of the attribute to import
- Returns: The requested attribute
- Raises:
AttributeErrorif the attribute doesn't exist
Discovery Functions
find_modules(package: Union[ModuleType, str], search_subpackages: bool = True, names_only: bool = False) -> Union[List[str], List[ModuleType]]
Discover all modules within a package.
- Parameters:
package: Package object or name to search insearch_subpackages: Whether to search nested packages (default:True)names_only: Return module names instead of objects (default:False). WhenTrue, avoids importing modules
- Returns: List of module objects or names
find_subclasses(base_class: Union[Type, str], search_in: Union[ModuleType, str], search_subpackages: bool = True, names_only: bool = False) -> Union[List[str], List[Type]]
Find all subclasses of a base class.
- Parameters:
base_class: Base class object or namesearch_in: Module, package, or path to search insearch_subpackages: Whether to search nested packages (default:True)names_only: Return class names instead of objects (default:False). WhenTrue, usespyclbrfor import-free static analysis
- Returns: List of subclass objects or names
find_instances(class_type: Type, search_in: Union[ModuleType, str], search_subpackages: bool = True) -> List[Any]
Find all instances of a specific class type.
- Parameters:
class_type: The class to find instances ofsearch_in: Module, package, or path to search insearch_subpackages: Whether to search nested packages (default:True)
- Returns: List of unique instances (deduplicated by object ID)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dynamic_imports-1.1.1.tar.gz.
File metadata
- Download URL: dynamic_imports-1.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ffe4d67e4b158dab0d61997749fb7f7b871f28afbe98d2ac7f48cd8543b6a07
|
|
| MD5 |
bec14012dc397b3d4603ff5327b40d4b
|
|
| BLAKE2b-256 |
5e6177d3bc53bebf5f971a503a96b97eebe5d9ff739b84f893ebaf856f5d3bbf
|
Provenance
The following attestation bundles were made for dynamic_imports-1.1.1.tar.gz:
Publisher:
release.yml on djkelleher/dynamic-imports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dynamic_imports-1.1.1.tar.gz -
Subject digest:
1ffe4d67e4b158dab0d61997749fb7f7b871f28afbe98d2ac7f48cd8543b6a07 - Sigstore transparency entry: 798919584
- Sigstore integration time:
-
Permalink:
djkelleher/dynamic-imports@54aefd147dceb909509750fe888ddee45f635801 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/djkelleher
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@54aefd147dceb909509750fe888ddee45f635801 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dynamic_imports-1.1.1-py3-none-any.whl.
File metadata
- Download URL: dynamic_imports-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2c91fa451ec37db109258c07a93790fd1f5e87c330ecb73ef63bc09ccd507df
|
|
| MD5 |
9891e2b225c173235b4490e53c2f1f24
|
|
| BLAKE2b-256 |
48a492ba54bc7d755eda182192f0c249d240367521e1b78baa07dd73beb291c8
|
Provenance
The following attestation bundles were made for dynamic_imports-1.1.1-py3-none-any.whl:
Publisher:
release.yml on djkelleher/dynamic-imports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dynamic_imports-1.1.1-py3-none-any.whl -
Subject digest:
c2c91fa451ec37db109258c07a93790fd1f5e87c330ecb73ef63bc09ccd507df - Sigstore transparency entry: 798919587
- Sigstore integration time:
-
Permalink:
djkelleher/dynamic-imports@54aefd147dceb909509750fe888ddee45f635801 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/djkelleher
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@54aefd147dceb909509750fe888ddee45f635801 -
Trigger Event:
push
-
Statement type: