Skip to content
Advertisement

Unresolved import in VSCode for Python

Trying to run a flask hello world program

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello World"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0..0.')

The Error:

unresolved import 'flask'

I have done Ctrl-Shift-P to select a Python interpreter but that did not seem to work.

The pip list seems to show flask so I am not sure why my import is not working.

Package       Version   
------------- ----------
appdirs       1.4.3     
CacheControl  0.12.6    
certifi       2019.11.28
chardet       3.0.4     
click         7.1.2     
colorama      0.4.3     
contextlib2   0.6.0     
distlib       0.3.0     
distro        1.4.0     
Flask         1.1.2     
html5lib      1.0.1     
idna          2.8       
ipaddr        2.2.0     
itsdangerous  1.1.0     
Jinja2        2.11.3    
lockfile      0.12.2    
MarkupSafe    1.1.1     
msgpack       0.6.2     
packaging     20.3      
pep517        0.8.2     
pip           20.0.2    
pkg-resources 0.0.0     
progress      1.5       
pyparsing     2.4.6     
python-dotenv 0.15.0    
pytoml        0.1.21    
requests      2.22.0    
retrying      1.3.3     
setuptools    44.0.0    
six           1.14.0    
urllib3       1.25.8    
webencodings  0.5.1     
Werkzeug      1.0.1     
wheel         0.34.2   

These are the settings from pyenv.cfg

home = /usr
implementation = CPython
version_info = 3.9.2.final.0
virtualenv = 20.0.17
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3.9

Advertisement

Answer

The reason is that the python environment used by the current VS Code terminal is different from the python displayed in the lower left corner of VS Code.

When using the “Jedi” language service, it does not show the problems in the code by default, and when using “Microsoft” or “Pylance” as the language service, VS Code will show the problems in the code.

Please use “python --version” to check the current python version used in VS Code. If it is different, please use Ctrl+Shift+` to open a new VS Code terminal.

Check: (pip show flask)

enter image description here

before:

enter image description here

after:

enter image description here

"python.languageServer": "Microsoft",

Reference: Python environments in VS Code.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement