I am using flask and have the following structure
<root>
manage_server.py
cas <directory>
--- __init__.py
--- routes.py
--- models.py
--- templates <directory>
--- static <directory>
--- formmodules <directory>
------ __init__.py
------ BaseFormModule.py
------ Interview.py
In routes.py, I'm trying to create an instance of the Interview class in the Interview module, like so
my_module = "Interview"
module = importlib.import_module('formmodules."+my_module)
I get an error here that says
ImportError: No module named formmodules.Interview
Some info about the init files:
/cas/formmodules/__init__.py is empty
/cas/__init__.py is where I initialize my flask app.
Let me know if it is helpful to know the contents of any of these files.
This is one of the classical relative vs absolute import problems.
formmodules
only exists relative tocas
, butimport_module
does an absolute import (as withfrom __future__ import absolute_imports
). Sinceformmodules
cannot be found viasys.path
, the import fails.One way to fix this is to use a relative import.
You might want to try with:
Note the
.
.The other option is to muck about with
sys.path
, which really isn't necessary, here.