Home > Mobile >  Flask module not found even though it's installed
Flask module not found even though it's installed

Time:03-17

I am following this python tutorial on flask, I have created the virtual environment and the code runs fine without issues. But when I open an interactive shell inside the virtual environment and type:

from app import db

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Repo\flask todo app\app.py", line 1, in <module>
    from flask import Flask, render_template
ModuleNotFoundError: No module named 'flask'

When I try to install flask it says it's already installed, I am not sure what I am doing wrong?

The full console output:

(venv) C:\Repo\flask todo app>python3
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Repo\flask todo app\app.py", line 1, in <module>
    from flask import Flask, render_template
ModuleNotFoundError: No module named 'flask'

The code in app.py:

from email.policy import default
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'

db = SQLAlchemy(app)


class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    completed = db.Column(db.Boolean, default=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return '<Task {}>'.format(self.id)


@app.route('/')
def index():
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

Note: I can see flask.exe under this path:

C:\Users\username\AppData\Local\Programs\Python\Python39\Scripts

But it seems like I am using python 3.10.2 version in the shell, not sure if it's relevant here.

CodePudding user response:

EDIT: I see that you are running python3 in your console... and that's it! Remember that you have to select a program to open. You should run python3 <your_program_name> instead.

Still, it will be easier for you to use the flask shell instead. To use it, run the flask shell command from your project directory while inside the virtual environment.

To access your project variables using the flask shell you have to create a shell_context_processor as follows:

# Insert this between your route definitions and the run statement

@app.shell_context_processor
def make_shell_context_processor():
    # The dictionary below will include any variable you want to access from the flask shell. Eventually you'll want to include here, for example, your database models
    return dict(db = db )

Miguel Grinberg (the author of an excellent Flask book and several courses) has an excellent entry in his blog where he explains the shell context processor in detail Do take a look at it (go to the "Shell Context" part, after the one titled "Playing with the database") and feel free to check his other entries if you have any other doubts with Flask.

CodePudding user response:

Do you ensure the installation is ok? If not, please check it and ensure it.install flask on windows

  • Related