
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
A simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.
Problem: I would like to be able to:
../
to get to the data.env
file without having to load them manuallySolution: 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
.
The package is tiny and continuosly maintained.
pip install rootutils
import rootutils
# 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 = rootutils.find_root(search_from=__file__, indicator=".project-root")
# find absolute root path (searches for directory containing any of the files on the list)
path = rootutils.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
rootutils.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)
)
Simplest usage with one-liner (combines find_root()
and set_root()
into one method):
import rootutils
root = rootutils.setup_root(__file__, dotenv=True, pythonpath=True, cwd=False)
Default root indicators (used when you don't specify indicator
arg):
[".project-root", "setup.cfg", "setup.py", ".git", "pyproject.toml"]
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.
Installation:
pip install autoroot autorootcwd
This adds root folder to pythonpath, sets PROJECT_ROOT env var, and loads variables from .env
:
import autoroot # root setup, do not delete
This also changes working directory to root:
import autorootcwd # root setup, do not delete
Autoroot exist for convenience and speed. For example, it's faster to just add import autorootcwd
at the beginning when creating new notebook.
Package page: https://github.com/ashleve/autoroot
This package is heavily inspired by:
https://github.com/chendaniely/pyprojroot
FAQs
Simple package for easy project root setup
We found that rootutils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.