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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dyn_import_utils-0.3.0.tar.gz.
File metadata
- Download URL: dyn_import_utils-0.3.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bde97b9f1b7bcb1dc3b5d18c00ef452b4b35953200c1c45702089be6e9490df3
|
|
| MD5 |
5b8aa187f17aed0e8422f85ac6c39dc1
|
|
| BLAKE2b-256 |
a381de406ffbf5cedc4b0d40f3972134a61fc01ae789fc18e276d23d02f37576
|
File details
Details for the file dyn_import_utils-0.3.0-py3-none-any.whl.
File metadata
- Download URL: dyn_import_utils-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d628177ca7ea00cd958441e79ca6bffd6c1a72f92aff74cd4e5137d4a8e7d3f4
|
|
| MD5 |
b28712a951c31600b26f57a33186db04
|
|
| BLAKE2b-256 |
20c1f439ae8e12a0cd70046dbba608f27e9eec28f11f97328cdbd712724f1e6f
|