-1

So I've been following along a course which deals with web services and I've encountered an issue with flask dependencies.

my requirements.txt looks like this:

PyMySQL==1.0.2
cryptography==37.0.4
Flask==2.2.2
flask_sqlalchemy==2.5.1
sqlalchemy < 1.4.0
flask_migrate==2.7.0
flask_script==2.0.6
sqlalchemy_utils==0.38.3

And my manage.py which I run with python manage.py db init looks like this

from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from configuration import Configuration
from models import database
from sqlalchemy_utils import  database_exists, create_database

application = Flask(__name__)
application.config.from_object(Configuration)

migrate = Migrate(application, database)

manager = Manager(application)
manager.add_command('db', MigrateCommand)


if __name__ == '__main__':
    if(not database_exists(Configuration.SQLALCHEMY_DATABASE_URI)):
        create_database(Configuration.SQLALCHEMY_DATABASE_URI)
    manager.run()

When I use flask 2.2.2 I get this error:

Traceback (most recent call last):
  File "/home/remax/PycharmProjects/V3/manage.py", line 3, in <module>
    from flask_script import Manager
  File "/home/remax/PycharmProjects/V3/venv/lib/python3.10/site-packages/flask_script/__init__.py", line 15, in <module>
    from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat'

But when I go back to flask 1.1.2 I get this ImportError: cannot import name 'escape' from 'jinja2'

I honestly don't know how to deal with this, I've been trying to find a solution all morning

kosingas
  • 61
  • 1
  • 7
  • Please show the full traceback to the `ModuleNotFoundError`, so we can help you tell what module is trying to import `flask._compat` (which is no longer a thing in Flask). – AKX Sep 01 '22 at 14:46
  • @AKX there it is – kosingas Sep 01 '22 at 14:50
  • Well, [there you go: `flask-script` has been deprecated since you don't need it since Flask 0.11.](https://github.com/smurfix/flask-script#deprecated) That's why it hasn't been updated to work with Flask 2. – AKX Sep 01 '22 at 14:52
  • @AKX Don't know honestly I was just typing along as the TA typed and it worked for him (with the 1.1.2 version). – kosingas Sep 01 '22 at 14:56
  • Right. If you go back to Flask 1.1.2, then you'll also need to downgrade Jinja2 to an older version. – AKX Sep 01 '22 at 14:57
  • @AKX which version do you suggest since I didn't see any changes being made to jinja2 in the course video? – kosingas Sep 01 '22 at 15:03
  • Flask 1.1.2's requirements say any 2.x version is compatible: https://github.com/pallets/flask/blob/1.1.4/setup.py#L57 – AKX Sep 01 '22 at 15:23
  • (Also, where does the jinja import error come from? Can you show that traceback?) – AKX Sep 01 '22 at 15:24
  • Take a look at this response for fixing it with Flask 1.1.2 - https://stackoverflow.com/a/71721006/14200476 – Darsana Sep 01 '22 at 15:28
  • adding these fixed it for me jinja2<3.1.0 itsdangerous==2.0.1 werkzeug==2.0.3 Thanks for your time and help – kosingas Sep 01 '22 at 15:39

1 Answers1

1

flask-script has been obsolete since Flask 0.11 added command support. It hasn't been updated to work with Flask 2, so if you want to use Flask 2, you'll need to get rid of it (and use Flask's native command support instead).

If you have upgraded Flask to 2.x and decide to downgrade to 1.x, you will need to install compatible versions of supporting packages (werkzeug, itsdangerous, jinja2). It might be easiest to just uninstall all packages from your virtualenv (you're using one, right?) and reinstall the correct versions, but you can also do it by hand.

AKX
  • 152,115
  • 15
  • 115
  • 172