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 when importing

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

injected_modules = [m1, m2]
module = dyn_import_utils.import_module("path/to/module.py", inject_symbols=injected_modules)

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!

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.1.tar.gz (5.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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dyn_import_utils-0.3.1.tar.gz
  • Upload date:
  • Size: 5.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.1.tar.gz
Algorithm Hash digest
SHA256 dacac38290e7dea103824ccffd21e1ef1e5a9da8f75addff6614a036181f0947
MD5 acc440a831eae6839d4c195ca56869c6
BLAKE2b-256 4e667556d046224e2379c5cef96edc7d225970893a92d488311dbff8035d62e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dyn_import_utils-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6de0f9b23def634650cca49cfb3b363c0bf167abbbc283401ced92191ab254b2
MD5 5b15cfc17deb95179a92bbbda5b73d58
BLAKE2b-256 ef6d116646d7610502e68b2a3525d65fefe5af26225e998590731dd723362421

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