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

11

17

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'
Gauffer answered 14/5, 2021 at 16:50 Comment(0)
A
23

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
Adiaphorism answered 15/5, 2021 at 3:30 Comment(5)
I just tried reinstalling Flask 2.0 with as suggest and still got the same error. You're original suggesting of using 1.1.2 seems to be the only solution there.Lien
For those newer to Flask and some of the db operations, in order to use the flask db... commands, you need to add AND init the Migrate object in your main Flask app file (i.e. app.py): from flask_migrate import Migrate ... migrate = Migrate(app, db) This lets you use Flask 2.0 (which has some cool new features) without downgrading.Hamza
Does this mean that we can't use Flask-Migrate on Flask v2+ ?Amorphism
@RyanAquino no, Flask-Migrate is available and working well! Just the Flask-Script is no longer used, being replaced by the better Flask CLI at all others take a look at the answer below: https://mcmap.net/q/686387/-flask-script-quot-modulenotfounderror-no-module-named-39-flask-_compat-39-quot and the Migrate Flask-Script to Flask 2.0 CLI articleVarini
Here is my environment info of python project. Python 3.10.8 Flask 3.0.0 Werkzeug 3.0.1 And here is entire codebase. https://github.com/SWS-5007/TellMeWhenToGo_Python And here is my error msg. ` File "C:\Users\HOME\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_script_init_.py", line 15, in <module> from flask._compat import text_type ModuleNotFoundError: No module named 'flask._compat' ` What is wrong in my manage.py file code? And how to fix this error?Trek
S
20

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 !!

Sourwood answered 7/6, 2021 at 15:39 Comment(5)
This should be the accepted answer, it solves the problem and someone should contact flask_script developers to fix the issue, because error persist in the latest Flask-Script==2.0.6 and Flask==2.0.1Sped
Flask-Script is not compatible with Flask 2.0.1 as of now. I have edited my answer.Adiaphorism
Hm, I did not checked compatibility issue but from flask_script._compat import text_type fix this error ModuleNotFoundError: No module named 'flask._compat'.Sped
This is a hack solution. What's the proper solution for flask-script to work out of box with the latest python ecosystem?Hypogeous
@KokHowTeh By not using it at all, and switching to the newer Flask tooling and other packages. Flask-Script is dead.Stepper
B
10

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

Burnout answered 15/12, 2021 at 22:54 Comment(0)
L
6

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

Leandraleandre answered 20/6, 2021 at 16:2 Comment(0)
P
4

I installed Flask 1.1.4

pip install "Flask==1.1.4"

And also werkzeug

pip install "werkzeug==1.0.1"
Pedanticism answered 22/5, 2021 at 21:13 Comment(0)
E
4

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.

Evalynevan answered 16/10, 2021 at 7:18 Comment(0)
L
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.

Levileviable answered 27/1, 2022 at 16:58 Comment(0)
B
-1

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"

Burton answered 29/6, 2021 at 6:12 Comment(1)
For readers of this answer, the main point of this answer is to use Flask==1.1.4 and Flask-Script==2.0.6, which is already mentioned in the other answers.Stepper
C
-1

Downgrading Flask worked for me

Updated requirements.txt with Flask==1.1.4

Coh answered 4/11, 2021 at 17:0 Comment(0)
F
-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
Fricassee answered 8/12, 2021 at 7:24 Comment(0)
W
-1

Quick and Permanent Fix:

As the error leads to line 15 of the libs/Flask_Script/__init__.py file, 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.

Wooton answered 6/5, 2022 at 21:50 Comment(1)
This is far from "permanent", because it requires modifying the Python package file (that's not portable when deploying/running your app in other environments) and you'll get stuck at Flask <= 1.x.Stepper

© 2022 - 2024 — McMap. All rights reserved.