Import Python modules from any file system path

Overview

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.

Owner
Danijar Hafner
I'm trying to build unsupervised intelligent machines and evaluate them in complex environments.
Danijar Hafner
File storage with API access. Used as a part of the Swipio project

API File storage File storage with API access. Used as a part of the Swipio project šŸ“ About The Project File storage allows you to upload and downloa

25 Sep 17, 2022
Fast Python reader and editor for ASAM MDF / MF4 (Measurement Data Format) files

asammdf is a fast parser and editor for ASAM (Association for Standardization of Automation and Measuring Systems) MDF (Measurement Data Format) files

Daniel Hrisca 440 Dec 31, 2022
Simple, convenient and cross-platform file date changing library. šŸ“šŸ“…

Simple, convenient and cross-platform file date changing library.

kubinka0505 15 Dec 18, 2022
Get Your TXT File Length !.

TXTLen Get Your TXT File Length !. Hi šŸ‘‹ , I'm Alireza A Python Developer Boy šŸ”­ Iā€™m currently working on my C# projects šŸŒ± Iā€™m currently Learning CSh

Alireza Hasanzadeh 1 Jan 06, 2022
This is a junk file creator tool which creates junk files in Internal Storage

This is a junk file creator tool which creates junk files in Internal Storage

KiLL3R_xRO 3 Jun 20, 2021
PaddingZip - a tool that you can craft a zip file that contains the padding characters between the file content.

PaddingZip - a tool that you can craft a zip file that contains the padding characters between the file content.

phithon 53 Nov 07, 2022
Remove [x]_ from StudIP zip Archives and archive_filelist.csv completely

This tool removes the "[x]_" at the beginning of StudIP zip Archives. It also deletes the "archive_filelist.csv" file

Kelke vl 1 Jan 19, 2022
Extract the windows major and minor build numbers from an ISO file, and automatically sort the iso files.

WindowsBuildFromISO Extract the windows major and minor build numbers from an ISO file, and automatically sort the iso files. Features Parse multiple

Podalirius 9 Nov 09, 2022
Read and write TIFF files

Read and write TIFF files Tifffile is a Python library to store numpy arrays in TIFF (Tagged Image File Format) files, and read image and metadata fro

Christoph Gohlke 346 Dec 18, 2022
Vericopy - This Python script provides various usage modes for secure local file copying and hashing.

Vericopy This Python script provides various usage modes for secure local file copying and hashing. Hash data is captured and logged for paths before

15 Nov 05, 2022
Search for files under the specified directory. Extract the file name and file path and import them as data.

Search for files under the specified directory. Extract the file name and file path and import them as data. Based on that, search for the file, select it and open it.

G-jon FujiYama 2 Jan 10, 2022
Uproot is a library for reading and writing ROOT files in pure Python and NumPy.

Uproot is a library for reading and writing ROOT files in pure Python and NumPy. Unlike the standard C++ ROOT implementation, Uproot is only an I/O li

Scikit-HEP Project 164 Dec 31, 2022
A Python script to organize your files in a given directory.

File-Organizer A Python script to organize your files in a given directory. It organizes your files based on the file extension and moves them into sp

Imira Randeniya 1 Sep 11, 2022
pydicom - Read, modify and write DICOM files with python code

pydicom is a pure Python package for working with DICOM files. It lets you read, modify and write DICOM data in an easy "pythonic" way.

DICOM in Python 1.5k Jan 04, 2023
Two scripts help you to convert csv file to md file by template

Two scripts help you to convert csv file to md file by template. One help you generate multiple md files with different filenames from the first colume of csv file. Another can generate one md file w

2 Oct 15, 2022
A platform independent file lock for Python

py-filelock This package contains a single module, which implements a platform independent file lock in Python, which provides a simple way of inter-p

Benedikt Schmitt 497 Jan 05, 2023
gitfs is a FUSE file system that fully integrates with git - Version controlled file system

gitfs is a FUSE file system that fully integrates with git. You can mount a remote repository's branch locally, and any subsequent changes made to the files will be automatically committed to the rem

Presslabs 2.3k Jan 08, 2023
A simple file sharing tool written in python

Share it A simple file sharing tool written in python Installation If you are using Windows os you can directly Run .exe file -- download If you are

Sachit Yadav 7 Dec 16, 2022
Python interface for reading and appending tar files

Python interface for reading and appending tar files, while keeping a fast index for finding and reading files in the archive. This interface has been

Lawrence Livermore National Laboratory 1 Nov 12, 2021
A Certificate renaming tool made for IEEE CS SBC, SJCE.

PDF Batch Renamer Made for IEEE CS SBC, SJCE How to use? Before using the python script, ensure that pytesseract, pdf2image, opencv and other supporti

Ashwin Kumar U 2 Nov 14, 2021