0

I have a file (dataset.json) that needs to be accessed from different places (tests.py, relative imports, and actual uses). I currently have a file structure similar to this:

project_dir
    |
    + - module_1
        |
        + - __init__.py
        + - abcd.py
        + - dataset.json
    + - xyz.py
    + - tests
        |
        + - tests_abcd.py

The issue that I'm trying to resolve is being able to read dataset.json from abcd.py. When I import abcd.py from xyz.py or tests_abcd.py, I get a FileNotFoundError because the path is different.

I've gone over the different methods for reading that file from this article, but it doesn't necessarily solve my problems. I also looked into this article as well. This SO post was helpful, but it didn't completely solve the problem.

What's the best way to define the file path? I don't want to write 50 different ways to access this file for each use case, and I wanted to learn what's the best way to handle problems like this.

Ali Abbas
  • 136
  • 1
  • 8
  • at every folder level create an empty __init__.py file, now you can trying importing like from project_dir.module_1.dataset.json – Surya Tej Oct 07 '20 at 06:12
  • That won't work because running `python xyz.py` would cause abcd to search in `project_dir` instead of module_1 – Ali Abbas Oct 07 '20 at 06:13

1 Answers1

0

Inside abcd.py:

from pathlib import Path
import json


def read_data():
    with open(Path(__file__).parent / 'dataset.json') as f:
        return json.load(f)

From xyz.py:

from module_1.abcd import read_data

my_data = read_data()

Or something along those lines, of course you don't have to return the data, you can just use it inside abcd.py.

__file__ is local to the script evaluating it, so xyz.py gets its own path and you can open the .json sitting next to it by prefixing the filename with the same parent path.

Grismar
  • 27,561
  • 4
  • 31
  • 54