{"id":18752881,"url":"https://github.com/ashleve/rootutils","last_synced_at":"2025-04-09T15:09:55.619Z","repository":{"id":38216423,"uuid":"494760140","full_name":"ashleve/rootutils","owner":"ashleve","description":"A simple python package to solve all your problems with pythonpath, work dir, file paths, module imports and environment variables.","archived":false,"fork":false,"pushed_at":"2023-05-19T13:04:08.000Z","size":34,"stargazers_count":161,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T15:09:47.700Z","etag":null,"topics":["cwd","environment-variables","file-paths","paths","pythonpath","root"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/rootutils/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashleve.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-05-21T11:10:27.000Z","updated_at":"2025-04-02T03:32:01.000Z","dependencies_parsed_at":"2022-09-26T20:52:46.741Z","dependency_job_id":"ebf62b7e-32eb-42d6-a872-f93e9b434fbb","html_url":"https://github.com/ashleve/rootutils","commit_stats":{"total_commits":40,"total_committers":4,"mean_commits":10.0,"dds":0.275,"last_synced_commit":"7122e8c47deb6c56c482f242119c82703f645383"},"previous_names":["ashleve/pyrootutils"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashleve%2Frootutils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashleve%2Frootutils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashleve%2Frootutils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashleve%2Frootutils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashleve","download_url":"https://codeload.github.com/ashleve/rootutils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248055282,"owners_count":21040157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cwd","environment-variables","file-paths","paths","pythonpath","root"],"created_at":"2024-11-07T17:23:00.634Z","updated_at":"2025-04-09T15:09:55.585Z","avatar_url":"https://github.com/ashleve.png","language":"Python","readme":"# rootutils\n\n[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)\n[![Tests](https://github.com/ashleve/rootutils/actions/workflows/test.yml/badge.svg?branch=main\u0026event=push)](https://github.com/ashleve/rootutils/actions/workflows/test.yml)\n[![Codecov](https://codecov.io/gh/ashleve/rootutils/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/rootutils)\n[![Build](https://github.com/ashleve/rootutils/actions/workflows/publish_package.yml/badge.svg)](https://github.com/ashleve/rootutils/actions/workflows/publish_package.yml)\n[![Issues](https://img.shields.io/github/issues/ashleve/rootutils)](https://github.com/ashleve/rootutils/issues)\n[![License](https://img.shields.io/github/license/ashleve/rootutils)](https://github.com/ashleve/rootutils/blob/main/LICENSE)\n[![Release](https://img.shields.io/pypi/v/rootutils)](https://pypi.org/project/rootutils/)\n[![PyPi](https://img.shields.io/pypi/dm/rootutils)](https://pypi.org/project/rootutils/)\n\nA simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.\n\n## Why rootutils?\n\n**Problem:** I would like to be able to:\n\n- Run my python scripts from anywhere\n- Always import python modules relatively to the project root directory\n- Always access files relatively to the project root so I don't have to specify a series of `../` to get to the data\n- Always have access to environment variables from `.env` file without having to load them manually\n- Have all the above benefits in notebooks even if they're nested in subdirectories\n\n**Solution:** The `rootutils` package provides a flexible way to setup the python project with a simple one-liner. It finds the project root based on the location of specified file name, e.g. `.project-root` or `.git`.\n\nThe package is tiny and continuosly maintained.\n\n## Setup\n\n```bash\npip install rootutils\n```\n\n## Usage\n\n```python\nimport rootutils\n\n# find absolute root path (searches for directory containing .project-root file)\n# search starts from current file and recursively goes over parent directories\n# returns pathlib object\npath = rootutils.find_root(search_from=__file__, indicator=\".project-root\")\n\n# find absolute root path (searches for directory containing any of the files on the list)\npath = rootutils.find_root(search_from=__file__, indicator=[\".git\", \"setup.cfg\"])\n\n# take advantage of the pathlib syntax\ndata_dir = path / \"data\"\nassert data_dir.exists(), f\"path doesn't exist: {data_dir}\"\n\n# set root directory\nrootutils.set_root(\n    path=path # path to the root directory\n    project_root_env_var=True, # set the PROJECT_ROOT environment variable to root directory\n    dotenv=True, # load environment variables from .env if exists in root directory\n    pythonpath=True, # add root directory to the PYTHONPATH (helps with imports)\n    cwd=True, # change current working directory to the root directory (helps with filepaths)\n)\n```\n\nSimplest usage with one-liner (combines `find_root()` and `set_root()` into one method):\n```python\nimport rootutils\nroot = rootutils.setup_root(__file__, dotenv=True, pythonpath=True, cwd=False)\n```\n\n## Defaults\n\nDefault root indicators (used when you don't specify `indicator` arg):\n\n```python\n[\".project-root\", \"setup.cfg\", \"setup.py\", \".git\", \"pyproject.toml\"]\n```\n\n## Autoroot\n\n`autoroot` is an experimental package that reduces `rootutils` to single import, without the need to execute any setup calls. This means just the act of importing this dependency (`import autorootcwd`) causes execution of recurrent search for `.project-root` file.\n\nInstallation:\n```bash\npip install autoroot autorootcwd\n```\n\nThis adds root folder to pythonpath, sets PROJECT_ROOT env var, and loads variables from `.env`:\n```python\nimport autoroot # root setup, do not delete\n```\n\nThis also changes working directory to root:\n```python\nimport autorootcwd # root setup, do not delete\n```\n\nAutoroot exist for convenience and speed. For example, it's faster to just add `import autorootcwd` at the beginning when creating new notebook.\n\nPackage page: https://github.com/ashleve/autoroot\n\n## Inspirations\n\nThis package is heavily inspired by:\n\nhttps://github.com/chendaniely/pyprojroot\n\nhttps://github.com/pashminacameron/py-repo-root\n\nhttps://github.com/EduardKononov/from-root\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashleve%2Frootutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashleve%2Frootutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashleve%2Frootutils/lists"}