Skip to content

michaelboerman/here

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Here

PyPI version License Python versions

here logo

A Python package that makes file paths easy and reproducible. Stop hardcoding paths and working directory headaches — use relative paths that work anywhere.

Tutorials

Getting Started

Install here using pip:

pip install here

Your First Path

Create a simple script that uses here() to resolve paths relative to your script:

from here import here

# Get the path to a data file relative to your script
data_file = here("data/input.csv")
print(data_file)
# Output: /absolute/path/to/your/project/data/input.csv

That's it! No matter where your script is called from, or what kind of python file from which it is called, this path will always point to the right place.

Working in Jupyter Notebooks

here() works seamlessly in Jupyter notebooks too:

from here import here
import pandas as pd

# Load data relative to your notebook location
df = pd.read_csv(here("data/results.csv"))

# Save outputs relative to your project root
df.to_csv(here("output/processed.csv"), index=False)

The path will be relative to your project root, even when working interactively.


How-To Guides

Organize Files in Your Project

A typical project structure works well with here():

my_project/
├── data/
│   ├── input/
│   └── output/
├── scripts/
│   ├── analysis.py
│   └── preprocessing.py
├── notebooks/
│   └── exploration.ipynb
└── results/

From any script or notebook, use relative paths:

from here import here

input_data = here("data/input/raw.csv")
output_data = here("data/output/processed.csv")
results = here("results/summary.txt")

Access Files from Nested Scripts

If you have scripts in subdirectories, here() still finds the project root:

my_project/
├── data/
├── scripts/
│   ├── main.py
│   └── utils/
│       └── helper.py

In helper.py, you can still access project-relative paths:

from here import here

# Even nested scripts can access project files
config_file = here("config/settings.json")

Navigate Up and Down

Use standard path notation to move up directories:

from here import here

# Go up from current location, then down into another folder
sibling_dir = here("../config")
parallel_project = here("../../other_project/data")

Explanation

The Problem: Fragile File Paths

Working with file paths in Python has challenges:

Hardcoded absolute paths break when files move:

# ❌ This breaks when you move your project
df = pd.read_csv("/Users/me/projects/my_work/data.csv")

Relative paths from os.getcwd() are unreliable:

# ❌ This breaks when you run from a different directory
df = pd.read_csv("data/input.csv")  # Only works if cwd is your project root

Jupyter notebooks and scripts behave differently:

# ❌ In notebooks, relative paths behave unexpectedly
# The working directory may not be where you think it is

Why here() Solves It

here() uses file-based path resolution instead of directory-based:

  1. It finds the directory containing your script or notebook
  2. It resolves paths relative to that location
  3. It always works, regardless of where the code is called from
# ✅ This always works, anywhere
data_file = here("data/input.csv")

How It Works

When you call here("path/to/file"):

  1. here() inspects the call stack to find which file called it
  2. It determines that file's directory
  3. It resolves your relative path from there
  4. It returns the absolute path

This approach means:

  • No setwd() needed — paths are always relative to your source file
  • Works in scripts, notebooks, and interactive shells — same behavior everywhere
  • Reproducible — paths don't depend on where the code is executed from
  • Portable — move your project and all paths still work

Reference

here(path="")

Resolves a file path relative to your script or notebook location.

Parameters:

  • path (str, optional): A relative path to resolve. Use / as separator. Default is empty string (returns the project root).
  • print_debug_info (bool, optional): If True, prints debug information about path resolution. Default is False.

Returns:

  • str: The absolute path to the file or directory.

Examples:

from here import here

# Get project root
root = here()

# Get a specific file
data_file = here("data/input.csv")

# Navigate with parent directories
config = here("../config/settings.json")

# Debug path resolution
here("data", print_debug_info=True)

Supported Environments:

  • Python scripts (.py files)
  • Jupyter notebooks (.ipynb files)
  • IPython interactive shells
  • Any Python environment with a call stack

get_calling_script_file_path(print_debug_info=False)

Returns the absolute directory path of the script or notebook that called this function.

Parameters:

  • print_debug_info (bool, optional): If True, prints debug information. Default is False.

Returns:

  • str: The absolute path to the directory containing the calling script.

Example:

from here import get_calling_script_file_path

current_dir = get_calling_script_file_path()
print(current_dir)
# Output: /absolute/path/to/your/project/scripts

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Python package that replicates the R package called "here"

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors