How to use relative paths in Python

Anchor file paths to your Python script with pathlib so it runs unchanged on your own computer and on LINCOMM.

This guide shows you how to write a Python script whose file paths keep working when you move it between your own computer and LINCOMM (Linux Community Servers). Python makes this clean: you anchor paths to the script's own location, and then it runs correctly from anywhere. For the concept behind this, see Understanding relative paths.

Prerequisites

  • A Python script and its data files, kept together in one project folder.
  • Python 3, which is what LINCOMM provides.

Steps

  1. Lay out your project so the data sits in a known place relative to the script:

    myproject/
      analysis.py
      data/
        transactions.csv
  2. At the top of analysis.py, anchor to the script's own folder using pathlib. The built-in name __file__ is the path to the running script, and .parent is the folder it lives in:

    from pathlib import Path
    
    here = Path(__file__).parent

    HERE now points to the folder containing analysis.py, on any computer.

  3. Build every data path from HERE, then read as normal. The / operator joins path parts safely on every operating system:

    import pandas as pd
    
    data_file = HERE / "data" / "transactions.csv"
    df = pd.read_csv(data_file)

    To reach a file in the parent folder, use HERE.parent, which is one level up.

  4. Run the script:

    python analysis.py

Verify it worked

Copy the whole myproject folder to LINCOMM, then run the script from a different folder than the project itself:

cd /mnt/aae/users/jdoe
python myproject/analysis.py

It should find transactions.csv and run without any path edits, even though you launched it from outside the project folder.

If something went wrong

  • FileNotFoundError: Check that the data folder was copied alongside the script, and that the names passed to Path match the real folder and file names exactly (Linux is case-sensitive).
  • Worked when run from inside the folder but not outside: You may be using a bare path like "data/transactions.csv" instead of anchoring to HERE. Build the path from HERE as shown in step 3.

Download sample Python files



Keywords:
how-to, python, relative paths, pathlib, lincomm, portability 
Doc ID:
161490
Owned by:
Eric D. in Agricultural & Applied Economics
Created:
2026-05-21
Updated:
2026-05-21
Sites:
Agricultural & Applied Economics