No module named app , While running Flask app

Viewed 1667

I'm new to using flask, I tried to execute a basic flask app in Visual-Studio-code . but I'm getting,

No Module named app

My code is:

from flask import Flask
app = Flask(__name__)


@app.route('/')
def index():
    return "Hello, World"


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

path :

Path

The output terminal:

PS C:\Users\Rakesh\Desktop\The project copy>  c:; cd 'c:\Users\Rakesh\Desktop\The project copy'; & 'C:\Python39\python.exe' 'c:\Users\Rakesh\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\launcher' '52116' '--' 'c:\Users\Rakesh\Desktop\The project copy\env\app.py' 
      
Serving Flask app 'app' (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: on
Restarting with stat
No module named app
2 Answers

You probably haven't set the settings.json file to allow visual studio code to run the application via virtualenv. Check out this link: https://code.visualstudio.com/docs/python/tutorial-flask (spoiler: you have to configure the variabile python.pythonPath to specify to vs code where is your python installation). A possible example of the settings.json file to configure visual studio code to use virtualenv:

{
    "python.pythonPath": "Scripts\\python.exe",
    "files.exclude" : {
        "**/.git" : true,
        "Lib" : true,
        "lib" : true,
        "Include" : true,
        "Scripts" : true,
        "**/__pycache__": true
    }
}

P.S. as mentioned by Edo Akse in the comment, it would be good practice not to put your own py files directly in the virtual environment folder

The path of app.py was inside the virtual environment , thus it is not working. Moving it out of that folder woks.

Related