Cannot import 'flask' in python

Question:

I am using the following code:

from flask import Flask, jsonify, request

However, I get an error from the word "flask":

Import "flask" could not be resolved

image 1

image 2

I tried this answer to reinstall the "flask" package but still got the error.

How can I fix this error? Appreciate if someone can advise. Thank you in advance!

Asked By: My Car

||

Answers:

First, enter pip3 show flask in the terminal. It will display something like this:

Name: Flask
Version: 2.2.2
Summary: A simple framework for building complex web applications.
Home-page: https://palletsprojects.com/p/flask
Author: Armin Ronacher
Author-email: [email protected]
License: BSD-3-Clause
Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
Requires: click, importlib-metadata, itsdangerous, Jinja2, Werkzeug
Required-by:

If the location says /Versions/3.7/ (maybe yours is different from mine), it means you added the package in Python 3.7 (maybe yours and mine are different). Remember the Python version, you will need it later.

Then, click + + P . After that, select Python: Select Interpreter. Then, choose the Python version where you added the package. The error may go away (mine went away).

Answered By: My Car

go to your vs code terminal then type this command

  1. sudo apt install python3-venv

  2. python3 -m venv my-project-env

  3. source my-project-env/bin/activate

  4. pip install flask

  5. after install CTRL+SHIFT+P and search for Python Interpreter
    enter image description here

  6. Select Your virtual Environment my-project-env which has created in above
    enter image description here

  7. now check the output. problem will be solved like this.
    enter image description here

Answered By: Saroar Zahan Sojib