Skip to main content

rela.tive imports from `__main__` and some test helpers

Project description

rela.tive imports in Python from __main__

Allow relative imports to work when running a script.

import rela; rela.tive('.')

No more dealing with ImportError: attempted relative import with no known parent package tracebacks.

How and Why?

It detects if __name__ == "__main__" and runs the code as if it were run using python -m.

This can make iterative code development and testing faster when developing in a single file inside a package.

You don't need to install the package to use the package when inside its development directory.

API Overview

rela.tive(to)

  • Allows relative imports in script running from __name__ == '__main__'
  • Parameters: to - a string of the package name or the number of dots to the top-level package.
  • Returns: the package's top-level module

rela.path(directory, top=True)

  • Ensures directory is found only once in sys.path, at the top or bottom of the list. Always runs.
  • Parameters: directory - a string with a relative or absolute path to add to sys.path.
  • Returns: the absolute path added to sys.path
    • can be used as a context manager that removes the path when done.

rela.if_main_run(module)

  • A hack to run other code instead of the current __main__ script.
  • Motivation: because you don't want to switch editor windows or your IDE can't configure its "run code" command.
  • Description: runs module if executing from __name__ == '__main__'
  • Parameters: module - a string with the relative module to run, e.g. '.__main__'
  • Raises: IfMainInterrupt exception when done, to stop execution of the rest of the script

unittest API helpers

@rela.test.case()

  • A decorator to filter tests from a unittest.TestCase class, used with rela.test.keep
  • Does not filter unless @rela.test.keep() is applied to at least one test method.
  • Motivation: Makes focusing on a particular test a lot easier
  • Parameters: None

@rela.test.keep()

  • A decorator applied to a particular test method, so that test.case keeps that test on the class.
  • Can be applied to multiple test methods.
  • Parameters: None

@rela.test.run(...)

  • A decorator to run a unittest.TestCase class if running from __main__.
  • Parameters: accepts TestRunner parameters
  • Raises: RelaTestRunDone

Examples

If your code is laid out like this:

project/
├── src
│   └── thing
│       ├── __init__.py
│       ├── __main__.py
│       ├── other.py
│       └── xyz.py
└── test
    └── test_thing.py

In __main__.py:

import rela; rela.tive('.')

Now you can run python __main__.py rather than python -m thing.

In test_thing.py:

import rela; rela.path('../src')
import thing.xyz   # now this imports

In other.py:

import rela; rela.tive('thing')  # '.' would work too
from . import xyz

Running python other.py now works rather than giving an ImportError.

Requirements

Minimum Python Version: 3.6, because of f-strings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

rela-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file rela-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rela-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for rela-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c74246b0de57140004330489339a11583ec409ef56db13a5f72d069ced74f489
MD5 66095baa2fc244ece3b7123f93cf1ab4
BLAKE2b-256 4e1544e07da098fed5434c94cad22c7939638edd84c4862d5b6e20a256c72a2c

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