0

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!

davidism
  • 121,510
  • 29
  • 395
  • 339
My Car
  • 4,198
  • 5
  • 17
  • 50
  • 1
    This question comes up constantly on Stack Overflow. It is usually due to having two Python versions installed, and the module in question being installed into one of the versions, and then the code being run by the other. Make sure that you know exactly what Python installation you are using in both cases. – CryptoFool Oct 10 '22 at 03:33
  • @CryptoFool What does two Python versions mean? – My Car Oct 10 '22 at 03:34
  • It is possible to install Python multiple times on the same machine. Like maybe one version came with your operating system, but then you installed a more recent one yourself. - This is particularly common if you are installing using `pip` at a command prompt, but are then running your code in an IDE. The IDE can be set up to be using a different version than your command line. – CryptoFool Oct 10 '22 at 03:35
  • 1
    Go to your IDE Python interpreter configuration and find out the path of the python used. Then on command prompt, navigate to that python directory, open python prompt and run import command. – Azhar Khan Oct 10 '22 at 03:36
  • @CryptoFool Are you talking about something like this: https://i.stack.imgur.com/F8TEV.png – My Car Oct 10 '22 at 03:37
  • @AzharKhan Are you talking about something like this: https://i.stack.imgur.com/F8TEV.png – My Car Oct 10 '22 at 03:37
  • There you go. You've 2 python interpreters `3.9` and `3.7`. Either choose the interpreter where Flask is installed. If you are not sure, then install Flask in both by navigating to that python folder and run `.\pip3 install flask` – Azhar Khan Oct 10 '22 at 03:40
  • @AzharKhan Which should I choose? – My Car Oct 10 '22 at 03:41
  • Does this answer your question? [python flask import error](https://stackoverflow.com/questions/14792605/python-flask-import-error) – Azhar Khan Oct 10 '22 at 03:46
  • @AzharKhan Thanks. When I choose 3.7, the error is resolved – My Car Oct 10 '22 at 03:47
  • @AzharKhan You can answer the question and I'll accept it – My Car Oct 10 '22 at 03:48
  • This question has been asked many times, and is possibly a duplicate. Please close it. – Azhar Khan Oct 10 '22 at 03:50
  • @AzharKhan I don't think this is a duplicate because I never saw any question about this with a solution for changing the interpreter – My Car Oct 10 '22 at 03:53
  • Does this answer your question? [How can I change the Python version in Visual Studio Code?](https://stackoverflow.com/questions/48135624/how-can-i-change-the-python-version-in-visual-studio-code) – Gino Mempin Oct 10 '22 at 08:52
  • @GinoMempin This is not a duplicate because the question is different although the answer is similar – My Car Oct 10 '22 at 08:54
  • That is the definition of a duplicate. Your question (and answer) is ultimately about making sure to select the correct Python environment on VS Code (whether by using "Select Interpreter" or by properly configuring the debugger's Python version). We don't need different Q&A for each and every Python package and Python version that shows a warning for "reportMissingImports". – Gino Mempin Oct 10 '22 at 08:57

1 Answers1

2

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: armin.ronacher@active-4.com
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).

My Car
  • 4,198
  • 5
  • 17
  • 50