Skip to main content

Import Python modules from any file system path.

Project description

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

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

pathimp-1.1.0.tar.gz (2.0 kB view details)

Uploaded Source

File details

Details for the file pathimp-1.1.0.tar.gz.

File metadata

  • Download URL: pathimp-1.1.0.tar.gz
  • Upload date:
  • Size: 2.0 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

Hashes for pathimp-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fde52b61d1f0f07e06753a4ba74990d4147ea31d6505546d378b66d479c849cf
MD5 06a6a1880c7d8185123d773f1986c9ea
BLAKE2b-256 da51850a5131ea8bdcb0c9c04f8769f1603f2e7ebaa04a1e7b6bd312cd4a805d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page