pyrootutils
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
.envfile 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
pip install pyrootutils
Usage
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)
)
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
Metadata
Release files for pyrootutils 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyrootutils-1.0.4.tar.gz | 5.1 kB | Without provenance | Details for pyrootutils-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyrootutils-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for pyrootutils-1.0.4-py3-none-any.whl |
Total release size: 11.0 kB
Release files / pyrootutils-1.0.4.tar.gz
| Download URL | pyrootutils-1.0.4.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f58aac67147bbab20938b7b783cb2f4670d6329a33c6640233854976538fc5f2
|
|
BLAKE2b-256 checksum How to use checksums |
377dc5f3fb44dc98a72249a17a49e28bce4aaa6758ce9e37e77648cdc1857baf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / pyrootutils-1.0.4-py3-none-any.whl
| Download URL | pyrootutils-1.0.4-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8bbf25425278693168ae03b167aa6509422dc21853a6ee01d5d5e40601a89f90
|
|
BLAKE2b-256 checksum How to use checksums |
1c6fed601aec632d908b86adbad0e1cc712f7073faf697134fb0465a3fbcc512
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for pyrootutils 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.