'Flask-Script: from flask._compat import text_type ModuleNotFoundError: No module named 'flask._compat'

When using Flask-Script I get an error when importing Manager.

I have installed with pip Flask and Flask-Script. How do I fix this?

manage.py

from flask_script import Manager

from main import app

manager = Manager(app)

@manager.command
def hello():

    print ("hello")

if __name__ == "__main__":
    manager.run()

main.py

from flask import Flask

app = Flask(__name__)

Error

(venv) raul@raul-PC:~/Proyectos Python/flask_script$ python3 manage.py hello
Traceback (most recent call last):
  File "manage.py", line 1, in <module>
    from flask_script import Manager
  File "/home/raul/Proyectos Python/flask_script/venv/lib/python3.8/site-packages/flask_script/__init__.py", line 15, in <module>
    from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat'


Solution 1:[1]

Did you update Flask to version 2.0.0 ? Degrade Flask to version 1.1.2 and it'll work.

EDIT
Instead of using with Flask-Script, you can simply use the below commands :

flask db init to initialize the database
flask db migrate to migrate new changes
flask db upgrade to upgrade and so on.

Solution 2:[2]

1 - I did installed modules manualy on PyCharm\settings\projet: myapp\python interpreter by selecting my interpreter and installing libs missed like Flask itself, flask-migrate and flask-script.

2 - On this specific error: from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat' -

It happened because the python searched on Flask._compat directory and It isn't there, so I changed like on below : (on flask_script/__init__.py)

Where:

from ._compat import text_type on original flask-script file

to :

from flask_script._compat import text_type

Then It works !!

Solution 3:[3]

I removed "from flask_script import Manager" and removed "manager = Manager(app)" from my main app.

And from the requirements.txt file.

Found on github: flask_script is not flask itself, but a (dead - repo archived) flask extension written by someone else.

Now my program runs with Flask 2.0.1

Solution 4:[4]

Downgrading to flask-script==2.0.5 worked for me, even while using Flask==2.0.2.

Solution 5:[5]

I installed Flask 1.1.4

pip install "Flask==1.1.4"

And also werkzeug

pip install "werkzeug==1.0.1"

Solution 6:[6]

Flask-Script is unlikely to create a compatible version for Flask 2.x or any other version after judging from their release history and last release. Miguel Grinberg explained this in his post and the fact that the introduction of Flask CLI in Flask 0.11 may have caused the death of Flask-Script (he's not wrong the dates do corroborate).

And you do not need to downgrade from the latest version of any tech you use to build your applications. Why should you? Newer versions come with new features, better security, improved efficiency and so on. And why stick to using a library that died four years ago?

What you ought to do instead (the no-shortcut method) is to shift to Flask CLI, use the Miguel Grinberg post I linked above and continue enjoying the new features of Flask 2.0.x. George Studenko's Medium article on the same might help you as well. It's a longer, trickier solution but ultimately more rewarding.

Good luck.

Solution 7:[7]

I try Flask-Script-

  1. my requirement.txt is

     click==7.1.2
     colorama==0.4.4
     Flask==1.1.4
     Flask-Script==2.0.6
     itsdangerous==1.1.0
     Jinja2==2.11.3
     MarkupSafe==2.0.1
     Werkzeug==1.0.1
    
  2. app.py

     from flask import Flask
     app = Flask(__name__)
    
     if __name__ == "__main__":
         app.run(debug=True)
    
  3. manage.py

     from flask_script import Manager
     from app import app
    
     manager = Manager(app)
    
     @manager.command
     def hello():
         print('test')
    
     if __name__ == "__main__":
         manager.run()
    
  4. run command - python manage.py hello

  5. My output showing as a result "test"

Solution 8:[8]

Downgrading Flask worked for me

Updated requirements.txt with Flask==1.1.4

Solution 9:[9]

you can try to degrade you flask version.

if you use pip, then it should be:

  1. python3 -m pip uninstall flask
  2. python3 -m pip install flask==1.1.4

Solution 10:[10]

As an alternative to downgrading Flask or Flask-Script, I used

from flask_login._compat import text_type

from the flask_login package, which I had in my project's dependencies, anyway.

Solution 11:[11]

Quick and Parmanent Fix:

As the error leads to line 15 of Virtual/libs/Flask_Script/init.py

Follow the steps below:

  1. Replace line 15

    from ._compat import text_type on original flask-script file

With: from flask_script._compat import text_type

Then,

  1. Downgrade flask: pip install flask==1.1.4
  2. Install compatible Markupsafe: pip install markupsafe==2.1.0

It is also good to ensure that these dependencies are installed:

    pip install flask_script
    pip install flask_bootstrap

Your project should work perfectly fine from this point.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 nobleknight
Solution 3
Solution 4 SunnyPro
Solution 5 hestellezg
Solution 6
Solution 7 Vijay
Solution 8 Ashwini Kondalakadu
Solution 9 SDA LS
Solution 10 Manu CJ
Solution 11 Alex M