Reputation: 14435
I have a basic yet annoying file reading issue.
I have a repo set up like this:
/repo/functions/feature.py
/script.sql
/repo/notebooks/task.ipynb
Within feature.py
, I have a function that reads the script.sql
and modifies it based off some input. Importing this function into a notebook within the /functions
directory works just fine.
However, when I have a notebook task.ipynb
in a different directory /notebooks
, the function now gives a "File not found" error. If I move the .sql file to /notebooks
then it works again, but this is far from ideal. I have also considered consolidating all static files into one directory, but this is annoying.
Is there a way to correctly route the file path lookup to always occur within the place it is inputted, regardless of where the function itself is imported and called?
Upvotes: 1
Views: 37
Reputation: 14435
I'll give an answer award to spatzlsberger, as he did point me the right way to How to make an "always relative to current module" file path?
This answer worked out for me. Leverage pathlib and modify the filepaths like so.
from pathlib import Path
here = Path(__file__).parent
fname = here/'script.sql'
Upvotes: 0
Reputation: 106
This is what you want: stackoverflow thread.
You can use the __file__
keyword within feature.py to access the directory where feature.py is. You can then build a path to script.sql reliably if it is always in the same directory as feature.py.
Upvotes: 2