Skip to main content

Utilities for locating project roots and keeping sys.path tidy

Project description

syspath-hack

syspath-hack provides small helpers for keeping sys.path predictable in scripts, notebooks, and tests. It resolves entries before adding them, avoids duplicates, and can locate your project root with a marker file such as pyproject.toml.

Installation

Install from PyPI with your preferred tool:

  • pip install syspath-hack
  • uv add syspath-hack

Quick start

Add the project root to sys.path so local imports work during ad-hoc scripts or notebooks:

from syspath_hack import add_project_root

add_project_root()  # finds the nearest pyproject.toml above the cwd

Prefer prepend_project_root() when you need imports to prioritise the working tree over installed copies of the package.

Both project-root helpers accept extra_paths for common subdirectories. For example, to prioritise the project and its src tree:

from syspath_hack import prepend_project_root

prepend_project_root(extra_paths=["src"])

Working in a GitHub Action? Use append_action_root() or prepend_action_root() to locate action.yml and automatically include scripts and src when they exist.

Working with temporary paths

When you need to add a directory only briefly, use temp_syspath to mutate sys.path inside a context manager and restore it afterwards:

from syspath_hack import SysPathMode, temp_syspath

with temp_syspath(["plugins"], mode=SysPathMode.PREPEND):
    import plugin_loader  # noqa: F401

For module-local imports, ensure_module_dir(__file__) adds the current file's directory to sys.path in one call, replacing the boilerplate Path(__file__).resolve().parent pattern.

Custom project markers

You can search for a different marker file and handle failures explicitly:

from syspath_hack import ProjectRootNotFoundError, find_project_root

try:
    repo_root = find_project_root("poetry.lock")
except ProjectRootNotFoundError as err:
    raise SystemExit(f"Could not locate the repository: {err}") from err
else:
    print(repo_root)

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

syspath_hack-0.3.0b1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

syspath_hack-0.3.0b1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file syspath_hack-0.3.0b1.tar.gz.

File metadata

  • Download URL: syspath_hack-0.3.0b1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for syspath_hack-0.3.0b1.tar.gz
Algorithm Hash digest
SHA256 af3cd2dffce27658d62089f9b24a2e74e93feeb4e605357030e23517f881e3f0
MD5 7b13af4017fc06e698ff112eb068f343
BLAKE2b-256 1de94d5fae7603e9ca08a3cc612a3bef09676189fcaaa5430c5481a15e070b45

See more details on using hashes here.

File details

Details for the file syspath_hack-0.3.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for syspath_hack-0.3.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 e45e4b0cacfe699a13da2c11ae12318fc9edb016f255f4b46118881393c4b431
MD5 cfa493b6e36e0fa4b71434c2d4173eed
BLAKE2b-256 75688cf274e0d4b392d45c573a08b2d80d19cf4d7591bb789526ad02e94cfcbd

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