Skip to main content

A simple package to get the absolute path of your Python project root

Project description

project_root_finder

project_root_finder is a simple Python package that provides a variable root which represents the absolute path to the root of the project in which it is installed.

Installation

You can install the package using pip:

pip install project_root_finder

Usage Examples

The project_root_finder package provides a simple way to get the absolute path to your project's root directory. This is useful for file operations that need to be relative to your project root, regardless of where the script is executed from.

Basic Usage

import project_root_finder
from pathlib import Path

# Access the project root path (returns a Path object)
project_root = project_root_finder.root

# Use it for file operations with pathlib
config_path = project_root / "config" / "settings.yaml"
data_path = project_root / "data" / "samples.csv"

# Or convert to string for other operations
log_path = str(project_root / "logs" / "application.log")

How It Works

The package determines the project root by:

  1. Starting from the location of its own __init__.py file
  2. Searching upward through parent directories for common project marker files:
    • .git (Git repository)
    • pyproject.toml (Python project config)
    • setup.py (Python package setup)
    • Pipfile (Pipenv configuration)
  3. If a marker is found, that directory is used as the project root
  4. If no markers are found in any parent directories, it falls back to the parent directory of the module

Custom Project Root Marker

The project_root_finder package also supports a custom project root marker file named .project-root-hook. If this file is present in any parent directory of the starting path, it will take precedence over other markers (e.g., .git, pyproject.toml, etc.) when determining the project root.

This feature is useful for projects that do not use standard markers or require a specific marker to define the root directory.

Running Tests

This package includes a comprehensive test suite to ensure it works correctly across different scenarios.

Prerequisites

Make sure you have the development dependencies installed:

pip install pytest
# Or if using pipenv
pipenv install --dev

Running the Tests

To run the test suite:

pytest

Or for more verbose output:

pytest -v

What the Tests Cover

The test suite verifies that project_root_finder:

  1. Returns a proper Path object
  2. Always returns an absolute path
  3. Correctly identifies project roots by detecting project marker files
  4. Falls back to the package's parent directory when no markers are found
  5. Properly checks all supported project markers in each parent directory

The tests use mocking to simulate different directory structures without requiring actual filesystem changes.

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

project_root_finder-1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

project_root_finder-1.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file project_root_finder-1.1.tar.gz.

File metadata

  • Download URL: project_root_finder-1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for project_root_finder-1.1.tar.gz
Algorithm Hash digest
SHA256 5099ec1980290bb82b8f8a9e119105e8e8cc52c566ca675d04e0d8716183b43a
MD5 8c927c6768d8159fd5f99b5036bb3847
BLAKE2b-256 e74401b62d482973fa5570516382dc69b9b33462710ad7d987bc3adef08a6e78

See more details on using hashes here.

File details

Details for the file project_root_finder-1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for project_root_finder-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51a0202712fb798cddbd060aec2ce981b230ff685d1c32e2382ec13e79c7b40b
MD5 e853650e322e932278f4ace3f91c9d50
BLAKE2b-256 54903a09aa459fc21c9f3bb0251d49d53714b7f9d5157daa58cb4e427aa3a0e2

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