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
Background
When using import, Python tries to find the module in sys.path. Additional
search directories can be added to sys.path or specified via the PYTHONPATH
environment variable. This solution works well in many cases. However, adding
directories to sys.path makes all modules inside the added directories
available to import. This pollution of the import path can lead to accidentally
importing undesired packages. The pathimp package allows importing packages
from a file system path without polluting the Python import path.
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.
Metadata
Release files for pathimp 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pathimp-1.4.0.tar.gz | 3.2 kB | Without provenance | Details for pathimp-1.4.0.tar.gz |
Release files / pathimp-1.4.0.tar.gz
| Download URL | pathimp-1.4.0.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c552a5a2140e15f2f4c43324dc7ba7b6183ca797d01b9b2d973c54585ecf247c
|
|
BLAKE2b-256 checksum How to use checksums |
9bca2f1bc0f77dc4562a0e4278a6e3778a55fd74cc9ce7032da028d017c194de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for pathimp 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.