Skip to main content

py-impala

Import packages and modules from arbitrary directories and files

Author:

Roman Neuhauser

Contact:

neuhauser@sigpipe.cz

Copyright:

This document is in the public domain.

Overview

Impala is a PEP302 protocol (sys.meta_path hook for the import statement) implementation allowing the user to import packages and modules from arbitrarily named directories and files.

Motivation

  • Comfort and freedom in development

  • Installed interface available without installation

Let’s say I’m developing a Python package called pyoneer. I want to lay the source code out like this:

README.txt
src/
  __init__.py
  some.py
  more.py
tests/
  ...

The question then is, how do I import pyoneer in the test files (<workdir>/tests/...) and have it load <workdir>/src/__init__.py? The default import mechanism requires packages to live in eponymous directories.

What’s the fuss about, you ask? I should simply rename the src directory to pyoneer or maybe src/pyoneer, no?

Indeed, this would be tolerable, at least with top-level packages. However, if I’m working on something that will be available as foo.bar.baz after installation, I certainly don’t want to wade through the desolate src/foo/bar to get to the source code.

Maybe I could import src in the tests instead? Well, tests are a form of documentation, and doubly so with doctest. “Proper” documentation (README.txt, etc) can also contain snippets which should be verifiable without the CUT being installed.

Impala to the rescue!

from os.path import abspath, dirname
import impala

root = abspath(dirname(__file__))

impala.register(dict(
  pyoneer = '%s/src' % root
))

import pyoneer

Description

impala.register(aliases)

aliases is a dict mapping from fully-qualified module/package names to paths to load from. To import a package p from path /a/b/c, aliases must include the key p with associated value /a/b/c, and /a/b/c/__init__.py must be a valid package entry point. To import a module m from path /f/g/h.py, aliases must include the key m with associated value /f/g/h.py.

Example:

from os.path import abspath, dirname
import impala

r = dirname(abspath(__file__))

impala.register({
  'p': '%s/a/b/c' % r,
  'p.q': '%s/f/g/h' % r,
  'p.q.m': '%s/k.py' % r,
})

import p
import p.q
import p.q.m

License

py-impala is distributed under the MIT license. See LICENSE for details.

Installation

Using pip from PyPI, the Python Package Index:

pip install impala

From a checkout or extracted tarball:

python setup.py install

Development

Source code and issue tracker are at Github:

https://github.com/roman-neuhauser/py-impala

Metadata

Release files for impala 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for impala 0.2
File Size Uploaded Provenance Actions
impala-0.2.tar.gz 8.8 kB Without provenance Details for impala-0.2.tar.gz

Release files / impala-0.2.tar.gz

Download URL impala-0.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f7b1f7f34af30ea9912c4afd65dc0c76c0ab30aa1fe64d0c81f72f31f5720523
BLAKE2b-256 checksum
How to use checksums
eb0bbd4f93aecc6c09f00085678f98d386f9a09ce5d8f8206110c1e549e35b0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for impala 0.2

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.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

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