Skip to main content

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)

Release files for syspath-hack 0.4.0

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

Source distribution (sdist)

Source distribution for syspath-hack 0.4.0
File Size Uploaded
syspath_hack-0.4.0.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for syspath-hack 0.4.0
File Interpreter ABI Platform
syspath_hack-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / syspath_hack-0.4.0.tar.gz

Download URL syspath_hack-0.4.0.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5e8897d6da1896b00c0018072c03b32242e655602fc574d0161c82ec01f7a9c4
BLAKE2b-256 checksum
How to use checksums
7dfbfd0a709b2c467103903a2588495710746cc3438896ca24ccec540265de47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.11

Release files / syspath_hack-0.4.0-py3-none-any.whl

Download URL syspath_hack-0.4.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc1d56da9f456a7d75f5c1469188ebeafe878e66e16fd3a4d6f981e432ec42fd
BLAKE2b-256 checksum
How to use checksums
455edb0d806e32005b4d32541ab35a957a30f06943c6d10c583c0355c44a0a43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.11

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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