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

Question:

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'
Asked By: moreno_asi

||

Answers:

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.

Answered By: Ashutosh Krishna

I installed Flask 1.1.4

pip install "Flask==1.1.4"

And also werkzeug

pip install "werkzeug==1.0.1"
Answered By: hestellezg

1 – I did installed modules manualy on PyCharmsettingsprojet: myapppython 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 !!

Answered By: Junior

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

Answered By: Henk Van Hoek

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"

Answered By: Vijay

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.

Answered By: Imran Said

Downgrading Flask worked for me

Updated requirements.txt with
Flask==1.1.4

Answered By: Ashwini Kondalakadu

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
Answered By: SDA LS

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

Answered By: SunnyPro

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.

Answered By: Manu CJ

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.

Answered By: Alex M
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.