Skip to main content

Forget about working directory errors

Project description

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? We have got a solution, keep reading.

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')

# assume you have stuff to save in a separate directory that does not exist yet.
# If `mkdirs` is set to True (False by default), all the non existing directories in the path 
# will be created before `from_root` returns. 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)

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 speaks 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.

There is a special case. If your package was installed by someone else via, for example, pip, the root directory is the one that is next to site-packages so there will be no conflicts with your usages of from-root and your users' ones

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

from-root-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

from_root-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file from-root-1.0.0.tar.gz.

File metadata

  • Download URL: from-root-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for from-root-1.0.0.tar.gz
Algorithm Hash digest
SHA256 726107b4cb9f3ba20ae8d0982e3f4e04dfaf37ffc8deb4b75e4f6d24b136f173
MD5 e628598817ea5ebd2e352d35c4170cd0
BLAKE2b-256 a3be31a45df95291e9117c986d76eaeb9eb2814224616986e41e823d73757cbb

See more details on using hashes here.

File details

Details for the file from_root-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: from_root-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for from_root-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 949f7da90dc3895f4ed02392c8de3092b0a6d93cc0eb1c3ab4a8ad82e1d9794e
MD5 c6bb5f3f4cf6dfb598eedd052061e8b7
BLAKE2b-256 362ae6f3476e0c1b299526281c84e5e9daa11fac16bfd195678cf78b6af6c525

See more details on using hashes here.

Supported by

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