Reputation: 215
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
Upvotes: 6
Views: 32887
Reputation: 1
Had same issue with me,
You just need to activate the virtual environment
try doing this-
#windows- ".\venv\Scripts\activate" #macOS/Linux- "source venv/bin/activate"
Now install flask once again
pip install flask
Upvotes: 0
Reputation: 11
It probably is the python interpreter that Flask is pointing to.
When global python interpreter is selected:
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/[email protected]/bin/python3.10: No module named flask
When venv python interpreter is selected:
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
Upvotes: 1
Reputation: 5426
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.
Upvotes: 0
Reputation: 2076
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.
Upvotes: 3
Reputation: 961
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!).
Upvotes: 1
Reputation: 31
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
Upvotes: 0
Reputation: 495
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.
Upvotes: 0
Reputation: 591
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).
Upvotes: 14
Reputation: 4664
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
Upvotes: 0