Skip to main content

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 can also be inferred based on the presence of any of the commonly used project root indicator files such as

  • .py-repo-root (empty file)
  • .git
  • .here (empty file)
  • requirements.txt
  • environment.yml
  • .flake8
  • setup.py
  • pyproject.toml

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.

Usage

Installation:

pip install py-repo-root

Usage

Get project root, using the default list of project root indicators

from pathlib import Path
from pyreporoot import project_root

project_root_dir = project_root(Path(__file__))

Specify a custom project root indicator (not included in the list above)

project_root_dir = project_root(Path(__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 pathlib import Path
from pyreporoot import project_root

sys.path.insert(0, str(project_root(Path(__file__), root_files='requirements.txt')))

Getting a relative path from the project root for a file:

from pathlib import Path
from projectroot import project_root

path_to_file_from_project_root = project_root(Path(__file__), root_files='requirements.txt').joinpath('/path/to/file.txt')

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

py-repo-root-1.1.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

py_repo_root-1.1.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file py-repo-root-1.1.2.tar.gz.

File metadata

  • Download URL: py-repo-root-1.1.2.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for py-repo-root-1.1.2.tar.gz
Algorithm Hash digest
SHA256 995ef3fd01f07d63b308bc092bbefc621139ccb6a7ea2162ae112f709e423d11
MD5 9604749c3d9572f4ddcb7c2dbad32f9a
BLAKE2b-256 4dfc91b60dc205e622a4fead71f5f2df35811fdd40b4a08b3f81a5b91caf7faf

See more details on using hashes here.

File details

Details for the file py_repo_root-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: py_repo_root-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for py_repo_root-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b3ff6737bfeb248547537d3eff400f848390f61f6801042f7f718ac74523530f
MD5 49579ba0ff362cf123b22be35c1764f4
BLAKE2b-256 205749d2348849003c307a75c95438ca4c5d13c9262c2719a28208fa3553371f

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