Skip to main content

Import packages from "wrongly" named directories

Project description

Author:

Roman Neuhauser

Contact:
neuhauser@sigpipe.cz

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!

import impala

impala.register(dict(
  pyoneer = '<workdir>/src'
))

import pyoneer

Description

impala.register(aliases)

aliases is a dict mapping from fully-qualified module/package names to paths to load from. To load 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 load a module m from path /k/l/m.py, aliases must include the key m with associated value /k/l/m.py.

Example:

import impala

impala.register({
  'p': '/a/b/c',
  'p.q': '/a/b/c/q',
  'p.q.m': '/a/b/c/q/m.py',
})

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

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

impala-0.1.1.tar.gz (8.8 kB view details)

Uploaded Source

File details

Details for the file impala-0.1.1.tar.gz.

File metadata

  • Download URL: impala-0.1.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for impala-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1d76e7a6c114818109f0d9fc1cf996dbfc5a0de50b73accbed3cad74d3639456
MD5 2975fcfb27ba3fb8a7cd899cd7706b5f
BLAKE2b-256 5cfb2cded2b842fb1a518fe0873269b93dfedbb0891d13e963a5d4249dac7ed1

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