Forums

Problems with Flask Tutorial: "from flask.ext.sqlalchemy import SQLAlchemy"

I'm following the excellent Flask tutorial by Giles Thomas. I'm near the end, creating the database tables. This is what happened.

My flask app.py app code says the following:

from flask.ext.sqlalchemy import SQLAlchemy

Per the instructions, from the bash console, I entered:

ipython3.4

Then, once ipython3.4 was running, I entered:

from flask_app import db

I subsequently got the following errors. Nor can I see the "comments" table in the mysql database.:

/usr/local/lib/python3.4/dist-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead.
.format(x=modname), ExtDeprecationWarning /usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.')

I tried to follow the suggestions in the error message and changed my code to:

from flask_sqlalchemy import SQLAlchemy

To no avail. Any ideas of how to move forward? In some of my searches, it seems like they are saying that there may be a newer version of Flask?

I'm glad you like the tutorial! Sorry to hear about the error, we don't upgrade system packages so I'm a bit confused you're seeing it.

That message should be a warning, anyway, so it certainly should have created the tables. What do you see if you run SHOW TABLES in a MySQL console started from the "MySQL" tab on your mikethechap$comments database?

You know how your dishwasher works perfectly under scrutiny? That's what my code was doing.

In any case, I was able now to finish the tutorial. Everything worked fine when I reran the code (although I did change the code in flask_app.py to

 from flask_sqlalchemy import SQLAlchemy

I'm not sure if that improved things or not. I looks like it might have. Other things I may have done wrong were perhaps related to:

  • Spacing? Note to self: Does Flask have rules about indentation?
  • Being in the wrong directory (somehow getting there)?
  • Not saving and then reloading appropriately?

In any case, a new day (reboot) often improves things. As it did here. Being new with this online approach to coding and Flask, what should I do (if anything

As far as the packages, is there anything I can/need to do about that in the future?

Thanks for your response! Again, excellent tutorial. Probably will convert over to a paying account at some point.

Mike Davis

if you were having weird version issues, make sure to check if you were using the right version of python (eg: 2.7 vs 3.4 etc), and also if you were using a virtualenv, if the webapp is setup with the virtualenv, and if you are running a script, if you have activated the virtualenv, and also if you have installed the correct packages into the virtualenv/correct python version.

Ah, the classic problem where code doesn't work until you show it to someone else! Happens to me at least daily :-)

The change you made to the import is a good one -- I should update the tutorial to reflect that. Re: your other points:

  • Spacing's always important in Python, because it defines the block structure of the code (eg. which of the following statements should be included as part of this function) but Flask specifically doesn't add any extra constraints.
  • Making sure you're in the right directory is definitely important, but I don't think that would cause import errors.
  • Saving and reloading -- not much I can add to that one :-)

Re: packaged -- that tutorial is designed to use the packages that are installed by default on PythonAnywhere, so that you don't need to worry about that. But in the future, especially if you want fine-grained control over exactly which versions of which packages you are using (we don't upgrade or downgrade installed packages unless you ask us to change your system image) then you may want to look into virtualenvs.