pyprojroot

0.3.0 · active · verified Thu Apr 16

pyprojroot is a Python library that facilitates project-oriented workflows by helping to find the project's root directory and construct paths relative to it. Inspired by R's `rprojroot` and `here` packages, it uses common project markers like `.git`, `setup.py`, or `requirements.txt` to locate the root. The current version is 0.3.0, and it's maintained with releases addressing improvements and compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `here()` to find the project root and then construct paths to files or directories relative to it. It assumes a project structure with a root marker (like `.git`) and uses `pathlib.Path` objects for robust path manipulation. The example creates temporary files to be runnable.

import os
from pyprojroot.here import here
from pathlib import Path

# Simulate a project structure for demonstration
# In a real project, you would have a .git or other marker in the root.
# And your data/notebooks would be actual directories.

# Create a dummy project root indicator (e.g., .git directory)
project_root_indicator = Path.cwd() / ".git"
project_root_indicator.mkdir(exist_ok=True)

# Create a dummy data directory and file
data_dir = here() / "data"
data_dir.mkdir(parents=True, exist_ok=True)
data_file = data_dir / "my_data.csv"
data_file.write_text("column1,column2\n1,A\n2,B")

print(f"Project root (found by pyprojroot): {here()}")
print(f"Absolute path to my_data.csv: {here('data/my_data.csv')}")

# Example of reading the data using a common library (e.g., pandas)
try:
    import pandas as pd
    df = pd.read_csv(here('data/my_data.csv'))
    print("\nData loaded successfully with pandas:")
    print(df)
except ImportError:
    print("\nInstall pandas (pip install pandas) to run the DataFrame example.")

# Clean up dummy project indicator and data
project_root_indicator.rmdir()
data_file.unlink()
data_dir.rmdir()

view raw JSON →