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!

16
  • 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. Commented Oct 10, 2022 at 3:33
  • @CryptoFool What does two Python versions mean? Commented Oct 10, 2022 at 3: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. Commented Oct 10, 2022 at 3: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. Commented Oct 10, 2022 at 3:36
  • @CryptoFool Are you talking about something like this: i.sstatic.net/F8TEV.png Commented Oct 10, 2022 at 3:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@MingJie-MSFT It says "You can accept your own answer in 2 days"
@MingJie-MSFT I would also like to accept this answer, but I can't. Stack Overflow stopped me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.