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

Uploaded Python 3

File details

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

File metadata

  • Download URL: dyn_import_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 0d8f804a8d8abd200f82d3a9f226a7ccef1e5df2f1bc87065d3e6ba5c45a4093
MD5 0547343f543f3b7836734225bc22db6a
BLAKE2b-256 a8c11958ad6a77a97eb6fb788ec6fb64d30c36ad4893fc190d545d3a82c1d36b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dyn_import_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7ed169a8f3eebf872bc0ab76d70ad5bdb37ff9573ab85192c52ab348a08baa
MD5 cfa860f48063de35b9e2aa41da6ce694
BLAKE2b-256 e9e85bd4f84f55aca02d967c47943a3c65da800f07a12cd3f6d09dc0046b357b

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