6

I want to debug an application using Python and Flask in VS Code. I have installed Flask and the app runs perfectly fine through cmd. But, when I try to debug it through VS Code, it gives the following error:

cd 'c:\Users\Aditi\CleanHandymanApp'; 
${env:FLASK_APP}='NewApp'; ${env:PYTHONIOENCODING}='UTF-8'; 
${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\Aditi\envs\CleanHandymanApp\Scripts\python.exe' 
'c:\Users\Aditi\.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py' '--client' '--host' 
'localhost' '--port' '63143' '-m' 'flask' 'run' '--no-debugger' '--no-reload'
No module named flask
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Lily
  • 169
  • 2
  • 2
  • 11
  • Ensure the path to your Python executable is on your PATH. Also make sure you have the Python extension installed in VS Code. – AJC24 Nov 17 '18 at 20:06
  • This was helpful for me to resolve the issue: https://stackoverflow.com/questions/31252791/flask-importerror-no-module-named-flask – karma Sep 06 '20 at 21:35
  • I have a similar issue, but the things suggested in this thread didn't work. Could you help me? https://stackoverflow.com/questions/74106140/no-module-named-flask-in-vscode-even-when-i-have-installed-flask – Swantewit Oct 18 '22 at 05:39
  • Does this answer your question? [Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module?](https://stackoverflow.com/questions/56658553/why-do-i-get-a-modulenotfounderror-in-vs-code-despite-the-fact-that-i-already) – Gino Mempin Sep 02 '23 at 03:42

8 Answers8

12

This error message can occur if you installed the python3 version of flask but Visual Studio Code tries to run your project with python2.

Make sure to select the correct version of python in the editor. This can be done by running the command Python: Select Interpreter from the Command Palette (Ctrl+Shift+P).

confusius
  • 571
  • 1
  • 4
  • 14
3

Activate your virtualenv and run

pip3 install -r requirements.txt

to reinstall all packages inside the venv.

For some reason VS Code thought I was missing all my packages the first time I debugged even though the app was running fine locally.

half of a glazier
  • 1,864
  • 2
  • 15
  • 45
1

Sometimes you can get this error if you loaded Flask into a folder which has sub-files. For instance, if you loaded flask into the parent folder with the virtual shell instance but you're running your code in the child file (let's say parent is called crypto_files and inside that is a python source code file called blockchain.py ), then in order to get flask to run properly you'd have to run the file like this:

python crypto_files/blockchain.py

This allows your machine to see Flask running inside crypto_files but also run blockchain.py .

OR, it's possibly you could just reload Flask into the sub(child)file... blockchain.py and then you'd run it from within the subfile.

This complication is mainly due to modern "virtual instances" and shells which are basically like creating a virtual computer-machine inside your ACTUAL hard machine. Flask does this to avoid running everywhere, and since Flask is modular it allows each of your projects to run different modular configurations of Flask to suit each project precisely. The alternative would be awful: you'd have to load the fattest version of Flask with dozens of add-ons for each project, and so all your git and all your projects would have tons of extra code. Flask is built to be very small at the core to avoid this problem (too verbose!).

John Pitts
  • 653
  • 6
  • 17
0

If you have installed flask in virtual environment, you should have activated it first.

source /path to env dir/bin/activate #in linux
workon 'name of env' #windows
Rarblack
  • 4,559
  • 4
  • 22
  • 33
0

Another option is add sys.path.append('d:/programas/anaconda3/lib/site-packages') in c:\Users\Aditi.vscode\extensions\ms-python.python-2018.10.1\pythonFiles\experimental\ptvsd_launcher.py

Being that "d:/programas/anaconda3/lib/site-packages" should be modified by your local python packages.

0

Use this command in the terminal instead of selecting run code:

python3 "insert your file name here without the quotes"

e.g.: python3 example.py

Aleksey Potapov
  • 3,683
  • 5
  • 42
  • 65
ablshk
  • 31
  • 1
0

I had a variant of the issue mentioned by @confusius. I had installed both Python 3.9 and Python 3.10. I had added Flask to Python 3.10. I had been using one vscode workspace which had selected Python 3.10. I started another project in a different vscode workspace and it had selected Python 3.9 by default, which I didn't notice because I thought it would select the same Python I had already selected in the other workspace.

M Katz
  • 5,098
  • 3
  • 44
  • 66
0

It probably is the python interpreter that Flask is pointing to.

When global python interpreter is selected:

global python

notebook:myproject leandro$  /usr/bin/env /usr/local/bin/python3.10 /Users/leandro/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 51448 -- -m flask run --no-debugger --no-reload /usr/local/opt/python@3.10/bin/python3.10: No module named flask

When venv python interpreter is selected:

venv python

notebook:myproject leandro$ source /Users/leandro/Desktop/myproject/my_venv/bin/activate (my_venv) notebook:myproject leandro$  /usr/bin/env /Users/leandro/Desktop/myproject/my_venv/bin/python /Users/leandro/.vscode/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 51748 -- -m flask run --no-debugger --no-reload * Serving Flask app '${myproject}/hello.py' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit

It can help: https://code.visualstudio.com/docs/python/tutorial-flask

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
leandro
  • 1
  • 1