maorui2k
maorui2k

Reputation: 53

ModuleNotFoundError: No module named 'flask_migrate'

I'm new to python+flask, and wanted to use flask to create a website. The IDE is Visual studio 2017, and I could run the program successfully with flasky.py as startup file. But in CLI, I constantly got this error.

(sms) C:\Document\Workspace\smsserver\smsserver>flasky.py
Traceback (most recent call last):
  File "C:\Document\Workspace\smsserver\smsserver\flasky.py", line 3, in <module>
    from flask_migrate import Migrate
ModuleNotFoundError: No module named 'flask_migrate'

The codes are:

import os
from os import environ
from flask_migrate import Migrate
from app import create_app, db
import app.models

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)
....

Here are modules installed in the venv.

(sms) C:\Document\Workspace\smsserver\smsserver>pip freeze
alembic==1.0.7
...
Flask==1.0.2
Flask-Bootstrap==3.3.7.1
Flask-Mail==0.9.1
Flask-Migrate==2.3.1
Flask-SQLAlchemy==2.3.2
....
SQLAlchemy==1.2.17
sqlalchemy-migrate==0.12.0
sqlparse==0.2.4
....

Is there anything I missed? Or any module confliction?

Upvotes: 5

Views: 23495

Answers (6)

Benson AD
Benson AD

Reputation: 61

Try reinstalling the Flask-Migrate. This worked for me

pip install Flask-Migrate

Upvotes: 5

Sarah Dwyer
Sarah Dwyer

Reputation: 529

sudo apt-get install python3-flask-migrate -y 

Upvotes: 2

Michael Maina
Michael Maina

Reputation: 11

Traceback (most recent call last): File "manage.py", line 4, in from flask_migrate import Migrate, MigrateCommand ImportError: cannot import name 'MigrateCommand' from 'flask_migrate'

use pip install flask-migrate==2.1.1 to solve

Upvotes: 1

Raj Darji
Raj Darji

Reputation: 1

  1. pip install Flask-Script==2.0.5

  2. pip install Flask-Migrate==1.2.0

  3. create manage.py file in your root directory and add following code:

    from flask_script import Manager
    
    from <your app name> import app,db
    
    import os
    
    from config import Config
    
    from flask_migrate import Migrate,MigrateCommand
    
    from flask import Flask
    
    from flask_sqlalchemy import SQLAlchemy
    
    
    app.config.from_object(Config)
    
    migrate = Migrate(app, db)
    
    manager = Manager(app)
    
    manager.add_command('db', MigrateCommand)
    
    if __name__ == '__main__':
    
        manager.run()
    
  4. Apply following commands:

    python manage.py db init 
    python manage.py db migrate
    python manage.py db upgrade
    

Upvotes: 0

Phil L.
Phil L.

Reputation: 2827

Watch out when working out of a python virtual env:

python3 -m venv .venv

After activating the private/local python environment by:

source .venv/bin/activate

Your PATH could be correctly updated but the shell hash may still point to the OLD python / flask location! (where the module is NOT installed)

Therefore to solve this you may just have to rehash at the shell by typing the following command:

hash -r

It solved this same problem for me.

Upvotes: 2

Miguel Grinberg
Miguel Grinberg

Reputation: 67492

From the CLI you need to run your script as follows:

python flasky.py

When you just run flasky.py Windows opens the script with the executable registered to handle the .py. extension on your system, which is your system-wide Python interpreter (i.e. not the interpreter associated with your virtual environment).

Upvotes: 1

Related Questions