Skip to main content

Tools for improving Python imports.

Project description

PyPI

imptools

Tools for improving Python imports.

Installation

pip3 install imptools

Docs

import_path()

Import a module from any path on the filesystem.

Usually, this would be achieved by adding the parent directory of the module to sys.path or the PYTHONPATH environment variable and then importing it normally. However, this pollutes Python's import path, which can lead to accidentally importing the wrong modules. The function import_path() avoids this problem by importing a package from a path on the filesystem without modifying the Python import path.

The module can be either a directory containing __init__.py or a single file.

Relative paths are resolved relative to the directory of the source file that calls import_path().

import imptools

my_module = imptools.import_path(
    '../path/to/my_module',  # Path to a module directory or single file.
    notfound='error',        # Raise 'error' or 'ignore' if not found.
    reload=False,            # Whether to import if already available.
)

import my_module  # Import statement also works.

enable_relative()

Enable relative imports for scripts that are not executed as module.

Usually, scripts that are part of a module and use relative imports must be run as python3 -m module.script. However, this requires being in the correct working directory and can be annoying. The enable_relative() function allows to execute those scripts normally as python3 script.py.

Since PEP 366, this can be achieved by setting the __package__ variable in the script and importing the package or making it available on the Pyhton import path. The enable_relative() function hides this behind a simple function that can be imported and called inside the script, before any relative imports.

import imptools

imptools.enable_relative()

# Relative imports...

Tests

python3 -m pytest tests

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

imptools-1.2.0.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file imptools-1.2.0.tar.gz.

File metadata

  • Download URL: imptools-1.2.0.tar.gz
  • Upload date:
  • Size: 4.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

Hashes for imptools-1.2.0.tar.gz
Algorithm Hash digest
SHA256 939a17d1fd82fdd39474ecae4deccbf8e14f8dde350847b7d0e790a7a4658430
MD5 ffb0eb4a925a8e110ef19ea967cb74bb
BLAKE2b-256 a8f882b799d25734219f5acf1ef201b25bebbffe2d36ca367b789e5f4b057cd8

See more details on using hashes here.

Supported by

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