Skip to main content

Load files easily using relative path names and simple heuristics

Project description

pyhere

Build Status Latest pypi version

A Python 2.x / 3.x equivalent of R's here package, drawing inspiration from chendaniely's pyprojroot package, but more closely mirroring the functionality within R's here. Relative file referencing has never been easier!

Installation

pip

You can install the latest stable version with pip via:

pip install pyhere

And if you want to be on the bleeding edge of development, get the latest version from github:

pip install --editable=git+https://github.com/joshpsawyer/pyhere.git#egg=pyhere

conda

Not in conda, yet - just install it from pip in your environment.

Usage

from pyhere import here

relative_dirA = here("your", "relative", "directory", "file.txt")
relative_dirB = here("your/relative/directory/file.txt")

pyhere uses simple heuristics to find a project's root directory. From Path.cwd(), it traverses upwards, looking for a possible root_indicator:

root_indicators = [
    ".here",
    "requirements.txt",
    "setup.py",
    ".vscode", # vscode project
    ".idea", # pycharm project
    ".git",
    ".spyderproject", # spyder
    ".spyproject", # spyder
    ".ropeproject" # rope
]

When found, it joins the arguments passed to here() to the rootpath and returns as a Path object. If it reaches the system root, it returns the system root and throws a warning.

For a concrete example, imagine the following directory structure:

\project\src\script.py
\project\data\data1.csv
\project\.here

If you call

data = here("data", "data1.csv")

from script.py, you'll get a Path object representing \project\data\data1.csv.

Project details


Download files

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

Files for pyhere, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size pyhere-1.0.0-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size pyhere-1.0.0.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page