Import Python modules from any file system path.
Project description
pathimp
Import Python modules from any file system path.
Installation
pip3 install pathimp
Usage
import pathimp
pathimp.import_module(
name='my_module', # Name of the module directory or file.
path='../path/to/parent/dir', # Path or list of paths to search.
notfound='error') # Raise 'error' or 'ignore' when not found.
import my_module
Details
After calling pathimp.import_module()
, the module is available in
sys.modules
and can be imported normally by later code. The function also
returns the module, allowing to use it directly without further import:
import pathimp
my_module = pathimp.import_module('my_module', '../path/to/parent/dir')
If the module is not found as a directory or file under the provided path, a
ModuleNotFoundError
is raied. The exception can be disable by passing the
notfound='ignore'
argument:
pathimp.import_module('my_module', '../path/to/parent/dir', notfound='ignore')
Whether the import succeeded can still be found out by looking at the return
value, which is either the module instance or False
if the module was not
found.
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
File details
Details for the file pathimp-1.2.0.tar.gz
.
File metadata
- Download URL: pathimp-1.2.0.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 225e486b9c3ca637d991cecd48ea6c08a43e988395b6802aa2c81b4faf384fe8 |
|
MD5 | 93d3dcfbb8e1e21c96212a301fa520e1 |
|
BLAKE2b-256 | 6de317f77b687c41f109fd87fc8ff48c6d47121268dfc324b823b1f1738b5c58 |