Skip to main content

Usage guide

Are you fed up with that annoying FileNotFoundError when your working directory turns out to be something different from what you expected? Or, maybe, you are looking for an easy and robust way of declaring paths to configs and any data files in your project? Yeah, that drives us all crazy.

The package is really tiny, there are two function:

  • from_root helps with declaring absolute paths relative to your project root
  • from_here comes in handy when you want to declare a path relative to the current file

They are dead simple to use.

Let's assume we have a project with the next structure:

├── .git  # OPTIONAL. See explanation below
├── .project-root  # OPTIONAL. See explanation below
├── config.json  
├── assets  
│ ├── animation.gif  
│ └── logo.png  
└── package  
    ├── __init__.py
    ├── data.csv    
    ├── script.py
    ├── FROM_HERE_DEMO.py
    └── inner_package  
      ├── FROM_ROOT_DEMO.py  
      ├── __init__.py
      └── insanely
          └── deep
              └── dir
                  └── file.txt

from_root examples:

# <PROJECT_ROOT>/package/inner_package/FROM_ROOT_DEMO.py
from from_root import from_root

CONFIG_PATH = from_root('config.json')

PACKAGE_DATA_PATH = from_root('package', 'data.csv')

# `from_root` returns pathlib.Path object
# so we can take advantage of its fantastic "/" syntax
ASSETS_DIR = from_root('assets')
ANIMATION_PATH = ASSETS_DIR / 'animation.gif'
LOGO_PATH = ASSETS_DIR / 'logo.png'

# no matter how deep it's located
FILE_TXT_PATH = from_root('package', 'inner_package', 'insanely', 'deep', 'dir', 'file.txt')

# If `mkdirs` is set to True (False by default), all *args will be treated as dir names 
# and created for you. 
# If a directory already exists, nothing happens.

import pickle

RESULTS_DIR = from_root('package', 'deep', 'results', 'dir', mkdirs=True)
results = {
    'ones': [1, 1, 1],
    'zeros': [2, 2, 2]
}
for name, data in results.items():
    path = RESULTS_DIR / f'{name}.pkl'
    # `FileNotFoundError` is not raised because `from_root` has created all non-existing directories
    with path.open('wb') as file:
        pickle.dump(data, file)

# WARNING: don't do this, you'll end up with data.json directory:
with from_root('results', 'data.json', mkdirs=True).open('w') as file:
    ...

# Do this instead:
with (from_root('results', mkdirs=True) / 'data.json').open('w') as file:
    ...

from_here examples:

# <PROJECT_ROOT>/package/FROM_HERE_DEMO.py
from from_root import from_here

# The only difference from `from_root` is that `from_here` allows you to declare relative paths
# I think the examples speak for themselves quite good. 
# Take a look at tree above and compare with `from_root` examples

CONFIG_PATH = from_here('data.csv')
FILE_TXT_PATH = from_here('inner_package', 'insanely', 'deep', 'dir', 'file.txt')

How does it work?

When from_root is called, folders in the current traceback are looked through one by one in order to find .git directory or .project-root file (might be empty; you have to create it on your own). The first one that contains at least one of them are considered as a root directory.

Metadata

Release files for from-root 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for from-root 1.3.0
File Size Uploaded
from_root-1.3.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for from-root 1.3.0
File Interpreter ABI Platform
from_root-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / from_root-1.3.0.tar.gz

Download URL from_root-1.3.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
da1359f5faabca367f685cac927cb2f307bb35c488fdd0361f963d6f1cd2674f
BLAKE2b-256 checksum
How to use checksums
ae305259cfafc8372df008a5605ca19aba9d560285471ee043f39cbc5a7b7fa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.9 Darwin/20.6.0

Release files / from_root-1.3.0-py3-none-any.whl

Download URL from_root-1.3.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7446a9b6481e668329cc11ad0a234fe4c83c63468c652e037d02846a75c726f8
BLAKE2b-256 checksum
How to use checksums
33a8451d0294d5d9ead3d26c25837df0588d1bcdd9235abf91e0ded629369921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.9 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page