Python utility for cleaner handling of paths
Project description
Introduction
This package standardizes the handling of paths in a Python project by
allowing all paths to be relative to the project repo root.
The package discovers the project root
based on the location of a special (empty) .py-repo-root
file.
The project root indicator can be overriden with a custom one, if desired.
This package was heavily inspired by pyprojroot. This package allows custom root file indicators, avoids recursive "/" is parent of "/" issue and can use the current file location to start walking the directory tree.
Setup
Installation:
pip install py-repo-root
Usage
Get project root, using the default list of project root indicators
from pyreporoot import project_root
project_root_dir = project_root(__file__)
Specify a custom project root indicator (not included in the list above)
project_root_dir = project_root(__file__, root_files='.my-root-indicator')
Another common usage pattern may be to add the project_root to PYTHONPATH for the purposes of running some scripts.
import sys
from pyreporoot import project_root
sys.path.insert(0, project_root(__file__))
Getting a relative path from the project root for a file:
from pathlib import Path
from projectroot import project_root
project_root_dir = project_root(__file__)
path_rel_to_project_root = Path(project_root_dir).joinpath('/path/to/file.txt')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for py_repo_root-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c66034931ebda96ddef8eb5e96b28b3493442aa52ac0ece4e73242c616d9897 |
|
MD5 | 043b64003bf285c82fd97665a0a31791 |
|
BLAKE2b-256 | 306c411ab4369d4bb6c2aaebcb63e89875917e23517fd319d99923e31707e68c |