Forums

Importing flask_sqlalchemy module Gives Unhandled Exception Error

Hi! I'm following the tutorial on how to back a flask app with a database. When including this line: from flask_sqlalchemy import SQLAlchemy in my python code, I reload and run the site which throws an Error code: Unhandled Exception. When I comment out this line, the site works! I checked the error logs last night and this morning. I've gotten two different errors (without having changed anything in the code since last night).

Last night the log read

Error running WSGI application
2018-07-27 18:24:29,653: ModuleNotFoundError: No module named 'flask_sqlalchemy'
2018-07-27 18:24:29,653:   File "/var/www/tblunt_pythonanywhere_com_wsgi.py", line 111, in <module>
2018-07-27 18:24:29,653:     from app import app as application  # noqa
2018-07-27 18:24:29,654: 
2018-07-27 18:24:29,654:   File "./app/__init__.py", line 3, in <module>
2018-07-27 18:24:29,654:     from flask_sqlalchemy import SQLAlchemy

Today the log reads:

Error running WSGI application
2018-06-16 02:46:26,303: ModuleNotFoundError: No module named 'btflaskwebapp'
2018-06-16 02:46:26,303:   File "/var/www/tblunt_pythonanywhere_com_wsgi.py", line 111, in <module>
2018-06-16 02:46:26,303:     from btflaskwebapp import app as application  # noqa

flask_sqlalchemy is already installed in my virtual env, and btflaskwebapp exists and works fine without that line of code. I know I need to import this module to use the database, any help is greatly appreciated!

[edit by admin: formatting]

Can I take a look at your code? We can see it from our admin interface, but we always ask for permission first.

Yes you can take a look!!

It doesn't look like flask_sqlalchemy is installed in your virtualenv. To avoid leaking potentially-secret information about your account I won't post the exact details here, but if you use the "File" page to navigate to the directory containing your virtualenv (as specified on the "Web" page), then go to the subdirectory "lib/python3.6/site-packages", you would see a directory called flask_sqlalchemy if it was installed, but there isn't one there.

If you start a bash console inside the virtualenv (using the link from the "Web" page) and run "pip install flask_sqlalchemy" then that should fix the problem.

when I run pip install flask_sqlalchemy from the console inside the virtualenv, i get a bunch of "requirement already satisfied" statements. this is why I thought that i already have flask_sqlalchemy installed. I'm not sure how to solve this

How did you create your virtualenv? Is it by any chance one that you created elsewhere, and then copied over to PythonAnywhere? That could cause a lot of confusing problems -- virtualenvs aren't designed to be portable.

I first created my virtual env on my local machine, then I uploaded the entire project to pythonanywhere. Is it possible to delete it and create a new one? How would I do this? Also, would I need to reinstall all modules once I create the new virtualenv?

That would explain the problem, yes.

Yes, the best way forward would be to delete it and create a new one. In fact, you don't even need to delete it if you like -- perhaps that's the best way to non-destructively test the "broken virtualenv" hypothesis :-)

So to create a new one, just run

mkvirtualenv newenv --python=python3.6

...from a bash console. That will create a new empty virtualenv called newenv using Python 3.6 in a well-known place (specifically /home/tblunt/.virtualenvs/newenv).

Next, yes, you need to reinstall all of the modules you need. The easiest way to do that is to run "pip freeze" in your local machine's virtualenv -- that will print out all of the installed packages and their respective versions, one-per-line. Put that output in a file called requirements.txt and put it in your code directory -- that's normal best-practice for Python code anyway.

Once that's done, and you have requirements.txt both locally and in your PythonAnywhere file storage, use workon newenv in a PythonAnywhere bash console to activate the env, and run pip install -r requirements.txt from the directory containing the requirements file. That will install all of your dependencies in one go.

Finally, change the virtualenv on the "Web" page. Because the new env is in a well-known place, you don't need to specify the full path -- just type in "newenv" and it will work out the rest.

thank you so much!!

No problem, glad I could help! Is everything working OK now?

Yes everything is working! I created the new virtual env and began to import all the modules from the requirements.txt file. I had no idea that importing them would take up so much space! We were at 98% file usage and not even halfway through the modules, so I deleted the virtual env all together. I’m not using one for this project.