-Booking
-components
-db.py
-scripts
-insert_data.py
How do I import db.py in insert_data.py? (I'm using python 3)
-Booking
-components
-db.py
-scripts
-insert_data.py
How do I import db.py in insert_data.py? (I'm using python 3)
from components import db
Should work as long as components
has a (potentially empty) __init__.py
in it
__init__.py
inside components
folderscripts
folder using from ..components import db
There are several options, and it helps to know how Python's modules and packages work (which is not always completely straightforward and intuitive):
One way is to explicitly modify the module search path in the beginning of your script:
import sys, os
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'components'))
import db
A less hacky way uses relative import, as described in Importing from a relative path in Python and Execution of Python code with -m option or not.