Skip to main content

Simple package for easy project root setup

Project description

pyrootutils

Python Tests Issues License Release PyPi

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

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
    pythonpath=True, # add root directory to the PYTHONPATH (helps with imports)
    cwd=True, # set current working directory to the root directory (helps with filepaths)
    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
)
import pyrootutils


# combines find_root() and set_root() into one method
root = pyrootutils.setup_root(
    search_from=__file__,
    indicator=".git" # indicator of the root directory
    pythonpath=True,
    cwd=True,
    project_root_env_var=True,
    dotenv=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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrootutils-1.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrootutils-1.0.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pyrootutils-1.0.2.tar.gz.

File metadata

  • Download URL: pyrootutils-1.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for pyrootutils-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7c56eddee063886a671de0791b295289f6ed67731310b465c6652460b01045f7
MD5 3f212f290c7e69968a88d4a36a6e8ce1
BLAKE2b-256 65154deed54b6d994fdb06466541fb87917d0a874bf6747f632a02c577be7c0b

See more details on using hashes here.

File details

Details for the file pyrootutils-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyrootutils-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for pyrootutils-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1a15d0492289f1de4caef3a87972d15317c4ed20c1816b2876ec252585f7bb18
MD5 202de77b0ed78a75ce559243db001185
BLAKE2b-256 068088a027238d4cb73873575888b7915f703b895441ffc8097177abe582bddb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page