Using cookiecutter-flask, I created a new blueprint/submodule called site
that is modeled after the user
submodule across all the relevant files, tests, etc. Tests are passing, DB created, and flask run
works, but when I try to enter flask shell
I get
AttributeError: module 'webapp.site' has no attribute 'models'
Here's the project structure and relevant bits from code:
--- Project Structure ---
├── assets
├── autoapp.py
├── webapp
│ ├── __init__.py
│ ├── app.py
│ ├── public
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ └── views.py
│ ├── settings.py
│ ├── site
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ └── views.py
│ ├── static
│ ├── templates
│ ├── user
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ └── views.py
│ ├── utils.py
│ └── webpack
├── tests
--- webapp/__init__.py
"""Main application package."""
--- webapp/user/__init__.py ---
from . import views
--- webapp/site/__init__.py ---
from . import views
--- webapp/app.py ---
from webapp import site, user
...
def register_shellcontext(app):
"""Register shell context objects."""
def shell_context():
"""Shell context objects."""
print(dir(site))
return {
'db': db,
'User': user.models.User,
--> 'Site': site.models.Site,
}
--- ERROR OUTPUT --- AttributeError: module 'webapp.site' has no attribute 'models'
I get the error output when I try to enter flask shell
. The user
module works fine, but site
barfs. If I open Python REPL, I can from webapp import site
and then do site.models
just fine. I can also change the import in app.py
to from webapp.site.models import Site
and that works - but I still would like to know why the other way isn’t working. I’ve also tried blanking out the site/__init__.py
(doesn't work) and importing models
to site/__init__.py
(which does work).
If I add a print(dir(site))
immediately after importing it in app.py
, I see
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'views']
I'm at a loss of what is going on here and why user
is working but not site
.