I am trying to keep a somewhat organized directory structure as I plan to add more and more scripts. So lets say I have a structure like this:
/src/main.py
/src/db/<all my DB conn & table manipulation scripts>
/src/api/<all my scripts that call different APIs>
My main.py script will include certain classes from db & api folders as needed. I have the blank _____init_____.py files in each folder so they are included fine. But say I want to include a class from the db folder in a script in the api folder? Like I would need to back up one dir somehow? The api scripts fail when I have a line like this in them:
from db.Conn import QADB
I am on v2.6.
Update: I have tried a relative import, but get this?
from ..db.Conn import QADB
^ SyntaxError:
invalid syntax
The way you have it setup is creating three different modules – this may or may not be what you want to do. If you want a common module which can manage different tasks you could arrange it as follows:
If you have it like this and want to use the API and database functionalities, you can start by saying:
Notice the way you had it: the name of your top “module” was
src(it was not a module because it did not have an__init__.pyfile).If you use many common functionalities in the top module (from either submodule) you can include them in the top
__init__.pyand simply callfrom mymodule import MyDBClass, MyAPIClass.Contents of top
__init__.py: