Skip to main content

A simple package for dynamic imports

Project description

dyn_import_utils

dyn_import_utils is a Python utility for dynamically importing modules and packages from file paths. It simplifies importing code that resides outside the standard Python module search path.


Features

  • Dynamically import Python modules from arbitrary file paths.
  • Load entire packages from directories containing __init__.py.
  • Lightweight and easy to integrate into existing projects.
  • The input path can be absolute or relative, in case of a relative path, the base path of the calling module is used
  • Symbols can be injected from other modules or dictionaries

Installation

Install dyn_import_utils via pip:

pip install dyn_import_utils

Usage

Lib import

import dyn_import_utils

Importing a Module

Use import_module to load a module dynamically from its file path:

# Dynamically load a module
module = dyn_import_utils.import_module("path/to/module.py")

# Use the module
print(module.some_function())

Importing a Package

Use import_package to load a package dynamically from its directory:

# Dynamically load a package
package = dyn_import_utils.import_package("path/to/package")

# Use the package
print(package.some_function())

Injecting Symbols

Both import_module and import_package support an optional inject_symbols parameter. Symbols are injected before the module/package code is executed, so they are available during the module's own initialization (e.g. at module top-level or in __init__.py).

Inject from a dictionary

Pass a dict mapping name strings to objects:

injected_classes = {
    "InjectedClass1": InjectedClass1,
    "InjectedClass2": InjectedClass2,
}
module = dyn_import_utils.import_module("path/to/module.py", inject_symbols=injected_classes)

Inside module.py, InjectedClass1 and InjectedClass2 are available as globals:

# module.py
obj = InjectedClass1()   # works — injected before execution

Inject from a module

Pass an existing module object — all its public attributes are injected:

import my_helpers
module = dyn_import_utils.import_module("path/to/module.py", inject_symbols=my_helpers)

Inject from a list of dicts and/or modules

Pass a list combining any mix of dicts and modules:

module = dyn_import_utils.import_module(
    "path/to/module.py",
    inject_symbols=[my_helpers, {"extra_value": 42}]
)

Using inject_symbols with import_package

inject_symbols works the same way for packages — symbols are injected into the package's __init__.py namespace before it executes:

package = dyn_import_utils.import_package(
    "path/to/package",
    inject_symbols={"injected_func": lambda x: x * 2}
)

Note: inject_symbols only applies to the top-level module or __init__.py. Submodules within a package are not automatically injected.


Adding a Directory to sys.path

Temporarily add a directory to the Python module search path:

dyn_import_utils.add_sys_path("path/to/directory")

Example

Example Module: hello.py

# hello.py
def greet():
    return "Hello, world!"

Dynamically Loading the Module

import dyn_import_utils

module = dyn_import_utils.import_module("hello.py")
print(module.greet())  # Output: Hello, world!

Example with Symbol Injection

# greeter.py  (to be dynamically imported)
message = injected_message   # 'injected_message' will be provided at import time

def greet():
    return message
import dyn_import_utils

module = dyn_import_utils.import_module(
    "greeter.py",
    inject_symbols={"injected_message": "Hello from the host!"}
)
print(module.greet())  # Output: Hello from the host!

License

dyn_import_utils is released under the MIT License. See the LICENSE file for more details.


Links

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

dyn_import_utils-0.3.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

dyn_import_utils-0.3.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file dyn_import_utils-0.3.2.tar.gz.

File metadata

  • Download URL: dyn_import_utils-0.3.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.1

File hashes

Hashes for dyn_import_utils-0.3.2.tar.gz
Algorithm Hash digest
SHA256 6bd453811ca15d942ae4dcff30b0ac4a358d3136909d50213de9cee2c6d82ba8
MD5 0e5a24b9dc9cc656be1e5634b9e973cc
BLAKE2b-256 8e56805c5122af242ef4ebc02989c402b0d52fab53e6813305ec2260d5f6128f

See more details on using hashes here.

File details

Details for the file dyn_import_utils-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for dyn_import_utils-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 764b5020e047dea7d9803ec542e7bd85d64b4221f258c4d693d57d364352b433
MD5 882684586b556bfa02b68a602e009b41
BLAKE2b-256 fde252dd5dc49747c3bfcbdc26ac93a8f2aa6c66f2008815edf2abd3820d6e80

See more details on using hashes here.

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