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

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())

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.2.0.tar.gz (4.8 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.2.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dyn_import_utils-0.2.0.tar.gz
  • Upload date:
  • Size: 4.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 4898b5c0ae3ed6b553a3ff980f3512096a2a7ff573ba67faa3d4457564f8f502
MD5 9bd2ad2e44f81aa2a10394b7d414f369
BLAKE2b-256 3c91ed4bba9b769a26b32255a24618f2da5f154440d82759b43ae185bdf6e383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dyn_import_utils-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee83dbc780b98b98eedb3e5a7b966b886cbab1351a759f12f38c87e358abe9ad
MD5 800247447bd96fe5ea9f2fed79c390be
BLAKE2b-256 11425dd97817015208f77461c67753cb774421e2f3bda18a6f688f36bff10ec1

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