# pyrootutils
[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml)
[![Codecov](https://codecov.io/gh/ashleve/pyrootutils/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/pyrootutils)
[![Build](https://github.com/ashleve/pyrootutils/actions/workflows/publish_package.yml/badge.svg)](https://github.com/ashleve/pyrootutils/actions/workflows/publish_package.yml)
[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues)
[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE)
[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
[![PyPi](https://img.shields.io/pypi/dm/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
A simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.
## Why pyrootutils?
**Problem:** I would like to be able to:
- Run my python scripts from anywhere
- Always import python modules relatively to the project root directory
- Always access files relatively to the project root so I don't have to specify a series of `../` to get to the data
- Always have access to environment variables from `.env` file without having to load them manually
- Have all the above benefits in notebooks even if they're nested in subdirectories
**Solution:** The `pyrootutils` 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`.
The package is tiny and continuosly maintained, so you can use it without worrying it gets deprecated in the future.
## Setup
```python
pip install pyrootutils
```
## Usage
```python
import pyrootutils
# find absolute root path (searches for directory containing .project-root file)
# search starts from current file and recursively goes over parent directories
# returns pathlib object
path = pyrootutils.find_root(search_from=__file__, indicator=".project-root")
# find absolute root path (searches for directory containing any of the files on the list)
path = pyrootutils.find_root(search_from=__file__, indicator=[".git", "setup.cfg"])
# take advantage of the pathlib syntax
data_dir = path / "data"
assert data_dir.exists(), f"path doesn't exist: {data_dir}"
# set root directory
pyrootutils.set_root(
path=path # path to the root directory
project_root_env_var=True, # set the PROJECT_ROOT environment variable to root directory
dotenv=True, # load environment variables from .env if exists in root directory
pythonpath=True, # add root directory to the PYTHONPATH (helps with imports)
cwd=True, # change current working directory to the root directory (helps with filepaths)
)
```
```python
import pyrootutils
# combines find_root() and set_root() into one method
root = pyrootutils.setup_root(
search_from=__file__,
indicator="pyproject.toml"
project_root_env_var=True,
dotenv=True,
pythonpath=True,
cwd=True,
)
```
## Inspirations
This package is heavily inspired by:
https://github.com/chendaniely/pyprojroot
https://github.com/pashminacameron/py-repo-root
https://github.com/EduardKononov/from-root
https://github.com/eddieantonio/project-paths
Raw data
{
"_id": null,
"home_page": "https://github.com/ashleve/pyrootutils",
"name": "pyrootutils",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7.0",
"maintainer_email": "",
"keywords": "",
"author": "ashleve",
"author_email": "ashlevegalaxy@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/37/7d/c5f3fb44dc98a72249a17a49e28bce4aaa6758ce9e37e77648cdc1857baf/pyrootutils-1.0.4.tar.gz",
"platform": null,
"description": "# pyrootutils\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/pyrootutils/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml)\n[![Codecov](https://codecov.io/gh/ashleve/pyrootutils/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/pyrootutils)\n[![Build](https://github.com/ashleve/pyrootutils/actions/workflows/publish_package.yml/badge.svg)](https://github.com/ashleve/pyrootutils/actions/workflows/publish_package.yml)\n[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues)\n[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE)\n[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)\n[![PyPi](https://img.shields.io/pypi/dm/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)\n\nA simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.\n\n## Why pyrootutils?\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 `pyrootutils` 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, so you can use it without worrying it gets deprecated in the future.\n\n## Setup\n\n```python\npip install pyrootutils\n```\n\n## Usage\n\n```python\nimport pyrootutils\n\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 = pyrootutils.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 = pyrootutils.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\npyrootutils.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\n```python\nimport pyrootutils\n\n\n# combines find_root() and set_root() into one method\nroot = pyrootutils.setup_root(\n search_from=__file__,\n indicator=\"pyproject.toml\"\n project_root_env_var=True,\n dotenv=True,\n pythonpath=True,\n cwd=True,\n)\n```\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\nhttps://github.com/eddieantonio/project-paths\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Simple package for easy project root setup",
"version": "1.0.4",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "bf5ceccdbc50556d9f8ddfcb5ac5c59a",
"sha256": "8bbf25425278693168ae03b167aa6509422dc21853a6ee01d5d5e40601a89f90"
},
"downloads": -1,
"filename": "pyrootutils-1.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bf5ceccdbc50556d9f8ddfcb5ac5c59a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7.0",
"size": 5831,
"upload_time": "2022-06-09T18:58:03",
"upload_time_iso_8601": "2022-06-09T18:58:03.184148Z",
"url": "https://files.pythonhosted.org/packages/1c/6f/ed601aec632d908b86adbad0e1cc712f7073faf697134fb0465a3fbcc512/pyrootutils-1.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "7231031223e7a8dbac9c09d4ea620f9b",
"sha256": "f58aac67147bbab20938b7b783cb2f4670d6329a33c6640233854976538fc5f2"
},
"downloads": -1,
"filename": "pyrootutils-1.0.4.tar.gz",
"has_sig": false,
"md5_digest": "7231031223e7a8dbac9c09d4ea620f9b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7.0",
"size": 5133,
"upload_time": "2022-06-09T18:58:04",
"upload_time_iso_8601": "2022-06-09T18:58:04.964196Z",
"url": "https://files.pythonhosted.org/packages/37/7d/c5f3fb44dc98a72249a17a49e28bce4aaa6758ce9e37e77648cdc1857baf/pyrootutils-1.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-06-09 18:58:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "ashleve",
"github_project": "pyrootutils",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "python-dotenv",
"specs": [
[
">=",
"0.20.0"
]
]
},
{
"name": "pytest",
"specs": [
[
">=",
"7.1.2"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
">=",
"3.0.0"
]
]
}
],
"lcname": "pyrootutils"
}