Forums

No module called flask_sqlalchemy

I'm trying to run portfolio site. Glenn helped me with clearing up certain issues but really confused again. Can someone help?

Error running WSGI application 2022-11-15 09:31:08,170: ImportError: No module named flask_sqlalchemy 2022-11-15 09:31:08,170: File "/var/www/wbsite_pythonanywhere_com_wsgi.py", line 16, in <module> 2022-11-15 09:31:08,171: from flask_app import app as application 2022-11-15 09:31:08,171: 2022-11-15 09:31:08,171: File "/home/elksie5000/mysite/flask_app.py", line 3, in <module> 2022-11-15 09:31:08,171: from flask_sqlalchemy import SQLAlchemy

Error from traceback in console:

/usr/local/lib/python3.9/site-packages/flask_sqlalchemy/init.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning(

Make sure that you have installed flask_sqlalchemy into the Python version/virtualenv that you are using in your web app. Also, decide whether you want to use SQLALCHEMY_TRACK_MODIFICATIONS and set it depending on your decision.

Hmm.

pip install flask_sqlalchemy Defaulting to user installation because normal site-packages is not writeable Looking in links: /usr/share/pip-wheels Requirement already satisfied: flask_sqlalchemy in /usr/local/lib/python3.9/site-packages (2.4.4) Requirement already satisfied: SQLAlchemy>=0.8.0 in /usr/local/lib/python3.9/site-packages (from flask_sqlalchemy) (1 .3.22) Requirement already satisfied: Flask>=0.10 in /usr/local/lib/python3.9/site-packages (from flask_sqlalchemy) (2.0.0) Requirement already satisfied: Werkzeug>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlal chemy) (2.0.1) Requirement already satisfied: click>=7.1.2 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlalc hemy) (7.1.2) Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlalch emy) (3.0.1) Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_s qlalchemy) (2.0.1) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.9/site-packages (from Jinja2>=3.0->Flask>=0. 10->flask_sqlalchemy) (2.0.1)

What gives?

See https://help.pythonanywhere.com/pages/InstallingNewModules/

Tried this to see if I could remove and reinstall. No use and just lots of red text.

15:22 ~$ pip uninstall flask_sqlalchemy Found existing installation: Flask-SQLAlchemy 2.4.4 Uninstalling Flask-SQLAlchemy-2.4.4: Would remove: /usr/local/lib/python3.9/site-packages/Flask_SQLAlchemy-2.4.4.dist-info/ /usr/local/lib/python3.9/site-packages/flask_sqlalchemy/ Proceed (y/n)? y ERROR: Exception: Traceback (most recent call last): File "/usr/local/lib/python3.9/shutil.py", line 806, in move os.rename(src, real_dst) OSError: [Errno 18] Invalid cross-device link: '/usr/local/lib/python3.9/site-packages/Flask_SQLAlchemy-2.4.4.dist-in fo/' -> '/tmp/pip-uninstall-59csw9d9' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 180, in _main status = self.run(options, args) File "/usr/local/lib/python3.9/site-packages/pip/_internal/commands/uninstall.py", line 85, in run uninstall_pathset = req.uninstall( File "/usr/local/lib/python3.9/site-packages/pip/_internal/req/req_install.py", line 672, in uninstall uninstalled_pathset.remove(auto_confirm, verbose) File "/usr/local/lib/python3.9/site-packages/pip/_internal/req/req_uninstall.py", line 386, in remove moved.stash(path) File "/usr/local/lib/python3.9/site-packages/pip/_internal/req/req_uninstall.py", line 275, in stash renames(path, new_path) File "/usr/local/lib/python3.9/site-packages/pip/_internal/utils/misc.py", line 324, in renames shutil.move(old, new) File "/usr/local/lib/python3.9/shutil.py", line 824, in move rmtree(src) File "/usr/local/lib/python3.9/shutil.py", line 718, in rmtree _rmtree_safe_fd(fd, path, onerror) File "/usr/local/lib/python3.9/shutil.py", line 675, in _rmtree_safe_fd onerror(os.unlink, fullname, sys.exc_info()) File "/usr/local/lib/python3.9/shutil.py", line 673, in _rmtree_safe_fd os.unlink(entry.name, dir_fd=topfd) PermissionError: [Errno 13] Permission denied: 'LICENSE.rst'

Tried removing the package and using the script:

15:50 ~$ pip3.9 install flask_sqlalchemy
Defaulting to user installation because normal site-packages is not writeable Looking in links: /usr/share/pip-wheels Requirement already satisfied: flask_sqlalchemy in /usr/local/lib/python3.9/site-packages (2.4.4) Requirement already satisfied: SQLAlchemy>=0.8.0 in /usr/local/lib/python3.9/site-packages (from flask_sqlalchemy) (1 .3.22) Requirement already satisfied: Flask>=0.10 in /usr/local/lib/python3.9/site-packages (from flask_sqlalchemy) (2.0.0) Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_s qlalchemy) (2.0.1) Requirement already satisfied: click>=7.1.2 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlalc hemy) (7.1.2) Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlalch emy) (3.0.1) Requirement already satisfied: Werkzeug>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.10->flask_sqlal chemy) (2.0.1) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.9/site-packages (from Jinja2>=3.0->Flask>=0. 10->flask_sqlalchemy) (2.0.1) 15:50 ~$

If you need a different version of a library than the one that we already have installed, then you will need to use a virtualenv. See the help page that I linked to earlier.

I don't want to use a virtualenv. I just want my original configuration. It is essentially one page.

If you do not want the virtualenv you need to use --user flag to install the desired package version in your home directory. Glenn posted the link to the help page about that above.

I've tried that. It doesn't work.

Could you clarify what exactly does not work and how?

I try to import:

I get this error:

Error running WSGI application 2022-11-15 09:31:08,170: ImportError: No module named flask_sqlalchemy 2022-11-15 09:31:08,170: File "/var/www/wbsite_pythonanywhere_com_wsgi.py", line 16, in <module> 2022-11-15 09:31:08,171: from flask_app import app as application 2022-11-15 09:31:08,171: 2022-11-15 09:31:08,171: File "/home/elksie5000/mysite/flask_app.py", line 3, in <module> 2022-11-15 09:31:08,171: from flask_sqlalchemy import SQLAlchemy

Are you running your web app with the same python version that you installed package for? What command did you use to install it?

When I created the app it was using Python 2.7, but upgraded to python 3.9. I think I removed the old version and used anaconda to install new version.

At the moment the site seems to be set to use Python 2.7. To fix the error you have right now, you'd just need to run

pip2.7 install --user flask_sqlalchemy

pip2.7 install --user flask_sqlalchemy

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Pyth on 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details abou t Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python- 2-support pip 21.0 will remove support for this functionality. Looking in links: /usr/share/pip-wheels Collecting flask_sqlalchemy Downloading Flask_SQLAlchemy-2.5.1-py2.py3-none-any.whl (17 kB) Requirement already satisfied: SQLAlchemy>=0.8.0 in ./.local/lib/python2.7/site-packages (from flask_sqlalch emy) (1.1.15) Requirement already satisfied: Flask>=0.10 in ./.local/lib/python2.7/site-packages (from flask_sqlalchemy) ( 1.1.4) Requirement already satisfied: itsdangerous<2.0,>=0.24 in ./.local/lib/python2.7/site-packages (from Flask>= 0.10->flask_sqlalchemy) (0.24) Requirement already satisfied: Jinja2<3.0,>=2.10.1 in /usr/local/lib/python2.7/dist-packages (from Flask>=0. 10->flask_sqlalchemy) (2.11.3) Requirement already satisfied: Werkzeug<2.0,>=0.15 in ./.local/lib/python2.7/site-packages (from Flask>=0.10 ->flask_sqlalchemy) (1.0.1) Requirement already satisfied: click<8.0,>=5.1 in ./.local/lib/python2.7/site-packages (from Flask>=0.10->fl ask_sqlalchemy) (7.1.2) Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2<3.0,> =2.10.1->Flask>=0.10->flask_sqlalchemy) (1.1.1) Installing collected packages: flask-sqlalchemy Successfully installed flask-sqlalchemy-2.5.1

But it still won't run. This is frustrating.

It looks like you didn't reload your website on the "Web" page to pick up the new module after installing Flask-SQLAlchemy. I reloaded it from our admin interface just now, and it looks like there's a new error -- you also need to install pandas for Python 2.7.

Is there a particular reason you're using 2.7? As the warning message in the pip install command above said, it's been deprecated since the start of 2020.

No, I would rather install python 3+. Thought I had. Sorry to be a pain. This is a great platform and haven't used python in a long time.

How do I upgrade?

On your system image ("glastonbury") Python 3 is already available from 3.5 to 3.9 (you can upgrade the system image to have 3.10, if you want, see this help page). To use appropriate Python version, you may need to specify it when in Bash console (e.g. pip3.9 or python3.8, or provide desired version when creating a virtual environment). For the web app, there's Python version setting on the Web page, in section Code. If you're using a venv for the web app, you might need also to create a new venv. Remember to reload the web app after making those changes.

Brilliant. That worked a treat. Now people can now see my work.

Excellent, thanks for confirming that!