I see this question has been asked before, but I still trying to get my head around working with python modules.
My app has a very basic structure:
app/
__init__.py
driver.py
dbloader/
__init__.py
loader.py
both __init__.py files are empty. driver.py only has one class Driver() and loader.py has only class in it Loader()
So, to test this setup, cd to inside the app/ directory. From here I start a python shell. I then try:
import dbloader which works (i.e. no errors). However, I’ve tried every permutation to get to instantiate Loader() inside loader.py to no avail. A few ones of the ones I’ve tried are:
from dbloader import loader
from dbloader.loader import Loader
I’ve also tried
importing just dbloader and then trying to instantiate as follow:
import dbloader
l = dbloader.Loader()
All to no avail. I believe reading elsewhere that the current directory and subdirectories are automatically included in the pythonpath when executing the python shell (is this true?)
Anyhow, any assistance would be much appreciated.
dbloaderby itself doesn’t have any reference to theLoaderclass. However you do it, you need to go through theloadernamespace. So, two possible ways:or
It helps to think about namespaces, rather than modules or classes.
Loaderis in thedbloader.loadernamespace, and to have access to that class you either need to import the class itself, or the module that contains it, into your current namespace.