Skip to main content

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 path
    • attr_name: Name of the attribute to import
  • Returns: The requested attribute
  • Raises: AttributeError if 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 in
    • search_subpackages: Whether to search nested packages (default: True)
    • names_only: Return module names instead of objects (default: False). When True, 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 name
    • search_in: Module, package, or path to search in
    • search_subpackages: Whether to search nested packages (default: True)
    • names_only: Return class names instead of objects (default: False). When True, uses pyclbr for 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 of
    • search_in: Module, package, or path to search in
    • search_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

dynamic_imports-1.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamic_imports-1.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

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

Hashes for dynamic_imports-1.1.1.tar.gz
Algorithm Hash digest
SHA256 1ffe4d67e4b158dab0d61997749fb7f7b871f28afbe98d2ac7f48cd8543b6a07
MD5 bec14012dc397b3d4603ff5327b40d4b
BLAKE2b-256 5e6177d3bc53bebf5f971a503a96b97eebe5d9ff739b84f893ebaf856f5d3bbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dynamic_imports-1.1.1.tar.gz:

Publisher: release.yml on djkelleher/dynamic-imports

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dynamic_imports-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_imports-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c2c91fa451ec37db109258c07a93790fd1f5e87c330ecb73ef63bc09ccd507df
MD5 9891e2b225c173235b4490e53c2f1f24
BLAKE2b-256 48a492ba54bc7d755eda182192f0c249d240367521e1b78baa07dd73beb291c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dynamic_imports-1.1.1-py3-none-any.whl:

Publisher: release.yml on djkelleher/dynamic-imports

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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