2

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
RakeshP-0304
  • 151
  • 1
  • 3
  • 10
  • 1
    https://stackoverflow.com/questions/22711087/flask-importerror-no-module-named-app Check it here. I also faced the same problem :) – ozmld Jun 16 '21 at 18:19
  • 1
    Note, you should absolutely not put your own py files in the virtual environment folder. This is against python coding conventions and it can make your life a lot harder as weird things can and will happen... – Edo Akse Jun 16 '21 at 21:33
  • @EdoAkse Oh... I moved the app.py out of virtual environment folder , Its working now .Thanks!! – RakeshP-0304 Jun 17 '21 at 14:16

2 Answers2

2

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

Matteo Pasini
  • 1,787
  • 3
  • 13
  • 26
0

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

RakeshP-0304
  • 151
  • 1
  • 3
  • 10