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.