Skip to main content

PyPI

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.

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)

Source distribution for pathimp 1.4.0
File Size Uploaded
pathimp-1.4.0.tar.gz 3.2 kB Details

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

Release history Release notifications | RSS feed

This release

1.4.0 This release

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page