miguelgrinberg/microblog

Error: Cannot import name 'app' from 'app'

DavidA2014 opened this issue · 2 comments

Hi Miguel

Probably my error, but I've downloaded your code for Chapter 4 and, when executing the interactive commands in section 4.9 of the book, I get:

>>> from app import app,db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'app' from 'app' (C:\<snip>\app\__init__.py)

Can you help with that please?

While I am here, I would also like to ask:

In models.py you define columns using this style:

username: so.Mapped[str] = so.mapped_column(sa.String(64), index=True, unique=True)

But another Flask tutorial (by a different developer) uses:

username = db.Column(db.String(20), unique=True, nullable=False)

which seems easier to understand. Would you like to comment on the difference?

I can't tell you what's wrong without seeing the code. Note that this tutorial is designed to be followed step by step from the start, if you are coming to chapter 4 without having done the previous chapter you may be missing something in your configuration.

Regarding the column definitions, the format I use in my tutorial is for SQLAlchemy version 2. The format you've seen in other tutorials is for SQLAlchemy 1. It can be used with SQLAlchemy 2, but it is considered legacy.

Thank you for your answer.