0

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.

Tyler_1
  • 176
  • 1
  • 2
  • 11
  • Could you tell why in the app.py file you are importing site and user from axon package? I can't see axon package in your project structure you provided. – Alex Mar 28 '18 at 21:02
  • @Alex - corrected that line, it should've been webapp – Tyler_1 Mar 28 '18 at 21:05
  • Is your __init__.py file in the webapp directory empty? If not could you also provide its content? – Alex Mar 28 '18 at 21:17
  • It is empty except for the docstring, added to OP. I also added a link to cookiecutter-flask to show what the code looks like otherwise. – Tyler_1 Mar 29 '18 at 00:27

0 Answers0