1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.