rootpath

0.1.1 · maintenance · verified Thu Apr 16

The `rootpath` library for Python offers automatic detection of a project's or package's root directory. It identifies the root by searching for common files and folders like `.git` or `requirements.txt` in parent directories. This is particularly useful for resolving Python's module import challenges by optionally adding the detected root path to `sys.path`. The current version is 0.1.1, and the library appears to be in a maintenance mode with its last PyPI update in 2019.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `rootpath.detect()` to find the project's root directory and `rootpath.append_to_sys_path()` to add it to `sys.path`, enabling simpler absolute imports within your project.

import os
import rootpath

# Example project structure for demonstration:
# /tmp/my_project/
# ├── .git (or requirements.txt, setup.py, etc.)
# └── src/
#     └── my_module.py

# Simulate running from a submodule (e.g., /tmp/my_project/src/my_module.py)
# In a real scenario, you'd run this from within your project directory.

# Detect the project root from the current file's location
# It will traverse up until it finds a common project marker.
project_root = rootpath.detect()
print(f"Detected project root: {project_root}")

# Optionally, add the project root to sys.path for simplified imports
rootpath.append_to_sys_path(project_root)
print(f"sys.path now includes: {project_root}")

# You can also override the detection pattern
# For example, if your root is marked by a specific file 'my_custom_root.txt'
# custom_root = rootpath.detect(pattern='my_custom_root.txt')
# print(f"Detected custom root: {custom_root}")

view raw JSON →