Getting Error: 'No module named flask' in VSCode even when I have installed flask

11,643

Solution 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!).

Solution 2

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).

Solution 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.

Solution 4

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
Share:
11,643

Related videos on Youtube

Lily
Author by

Lily

Updated on September 16, 2022

Comments

  • Lily
    Lily over 1 year

    I want to debug an application using Python and Flask in VSCode. I have installed Flask and the app runs perfectly fine through cmd. But, when I try to debug it through VSCode, 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
    

    Can you please help me.