I have a directory structure that looks like this:
foo/
__init__.py
db.py
database.db
main.py
Now, in db.py
, I read the contents of the database:
open('database.db').read()
This works as long as I am running db.py directly. However, when I try to import the foo
package in main, and then run db.py, it fails with a file not found error.
How can I avoid this? (Preferably without making any changes to the modules in the foo
package)
(Note: I am aware of a solution like this Relative paths in Python but that would entail a lot of modifications to my foo
package)
EDIT 1: Is it not possible to add something to __init__.py
so that python can find my files?
EDIT 2: As another small example, consider what happens when I start to have nested packages:
foo/
baz/
__init__.py
mod.py
modules.json
__init__.py
db.py
database.py
main.py
Now, if main.py
uses the foo
package, and db.py
requires the baz
package, then I need to start making more complicated changes in the modules of the baz
as well as the foo
package, so that the file reads succeed.
This kind of a situation is happening when I need to use a git module that has several nested submodules. Obviously, it's not very convenient to make so many changes.