published on in josbos

Flask-Script: "ModuleNotFoundError: No module named 'flask._compat'"

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

I have installed 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' 

11 Answers

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

Instead of using Flask-Script, you can simply use the following commands:

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

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

For this specific error:

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

It happened because Python searched for a Flask._compat directory but it isn't there, so I updated flask_script/__init__.py and changed the code:

On the original flask-script file:

from ._compat import text_type` 

To:

from flask_script._compat import text_type 

Then it works !!

5

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

0

I removed

from flask_script import Manager 

and

manager = Manager(app) 

from my main app.

And removed Flask-Script 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

0

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.

I installed Flask 1.1.4

pip install "Flask==1.1.4" 

And also werkzeug

pip install "werkzeug==1.0.1" 

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.

I try Flask-Script-

  • 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 
  • app.py

     from flask import Flask app = Flask(__name__) if __name__ == "__main__": app.run(debug=True) 
  • 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() 
  • run command - python manage.py hello

  • My output showing as a result "test"

  • 1

    Downgrading Flask worked for me

    Updated requirements.txt with Flask==1.1.4

    Quick and Permanent Fix:

    As the error leads to line 15 of the libs/Flask_Script/__init__.py file, follow the steps below:

  • Replace line 15
    from ._compat import text_type on original flask-script file 
    with:
    from flask_script._compat import text_type 
  • Then,

  • Downgrade flask:
    pip install flask==1.1.4 
  • 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.

    1

    You can try to downgrade you Flask version.

    If you use pip, then it should be:

    python3 -m pip uninstall flask python3 -m pip install flask==1.1.4 

    ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobXBtY219doKOn6Oaq5tiwKS%2ByKmrZqWfmcKtsc2oq5%2BnpaOxpr7RqKlmpp9iurCw1KWcZqaRorKlecWlmKyjXZi8rrzArQ%3D%3D