简体   繁体   English

无法从源代码解析导入 flask

[英]import flask could not be resolved from source pylance

I'm learning Python and part of the course setting up a webserver using Flask.我正在学习 Python 以及使用 Flask 设置网络服务器的课程的一部分。 I followed the steps as per the Flask installation documentation and for some reason the flask module is underlined as shown below.我按照 Flask 安装文档执行了这些步骤,并且由于某种原因,flask 模块带有下划线,如下所示。 When I hover my mouse, I get additional information as below.当我使用鼠标 hover 时,我会得到以下附加信息。

import flask could not be resolved from source pylance无法从源代码解析导入 flask

The server is running fine though.服务器运行良好。 Should i be ignoring the notification?我应该忽略通知吗? If not what dependency have i missed?如果不是我错过了什么依赖?

Below is the code to setup the server下面是设置服务器的代码

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

在此处输入图像描述

在此处输入图像描述

When I did not install the module " flask ", I ran into the problem you described:当我没有安装模块“ flask ”时,我遇到了你描述的问题:

在此处输入图像描述

The reason is that the module " flask " is not installed in the Python environment we currently use in VSCode.原因是我们目前在VSCode中使用的Python环境中没有安装模块“ flask ”。

Please use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, it will automatically enter the currently selected environment, and then use the command " pip show flask " to check the installation location of the module "flask":请使用快捷键Ctrl+Shift+`打开一个新的VSCode终端,它会自动进入当前选择的环境,然后使用命令“ pip show flask ”查看模块“flask”的安装位置:

在此处输入图像描述

If it still shows that the module could not be resolved, it is recommended that you reinstall the module " flask ".如果仍然显示模块无法解析,建议您重新安装模块“ flask ”。

In VS Code, Go to "Python: Select interpreter" by Ctrl + Shift + P. Choose python interpreter ('base': conda)在 VS Code 中,通过 Ctrl + Shift + P 将 Go 转换为“Python:Select 解释器”。选择 python 解释器('base':conda)

I experienced the same situation until I changed the virtual environment of my VS Code to indicate the correct value that I should use:在我更改 VS Code 的虚拟环境以指示我应该使用的正确值之前,我遇到了同样的情况:

我的 VSCode 的屏幕截图

  1. Firstly Create a Virtual Environment on your terminal首先在您的终端上创建一个虚拟环境
  2. then install your flask by pip install flask然后通过pip 安装 flask 安装 flask
  3. after install CTRL+SHIFT+P安装后CTRL+SHIFT+P
  4. Search Python Interpreter搜索Python 口译员
  5. Select Your virtual Environment Select 您的虚拟环境

Problem Will bi fixed.问题将修复。 I have also faced same problem.我也面临同样的问题。 but I have fixed it following this procedure但我已经按照这个程序修复了它

In case you are using a virtual environment;如果您使用的是虚拟环境;

  1. Create a virtual environment.创建虚拟环境。

    python3.9 -m venv --without-pip virtual

  2. Activate the virtual environment.激活虚拟环境。

    source virtual/bin/activate

  3. Install the pip for the created virtual environment.为创建的虚拟环境安装 pip。

    curl https://bootstrap.pypa.io/get-pip.py | python

  4. Install flask into the virtual env.将 flask 安装到虚拟环境中。

    pip install flask

  5. Create the python file.创建 python 文件。 For your case,对于你的情况,

    touch server.py

  6. Open file and import the module打开文件并导入模块

  7. If it underlines again, install pip again while the.py file is still open.如果它再次下划线,请在 .py 文件仍处于打开状态时再次安装 pip。

    pip install flask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从源 Pylance (reportMissingModuleSource) 解析导入“flask” - Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) 无法从源 Pylance(reportMissingModuleSource)解析导入“matplotlib” - Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource) 在 Windows 10 上使用 Python 3.9.2 在 VS 代码中无法解析/无法解析导入 - Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 导入“numpy”无法解决 Pylance - Import "numpy" could not be resolved Pylance 导入 Manim 无法解决 Pylance - Import Manim could not be resolved Pylance 在 Pylance 中无法解析导入“布朗尼” - Import "brownie" could not be resolved in Pylance "导入“spacy”无法解决 Pylance (reportMissingImports)" - Import "spacy" could not be resolved Pylance (reportMissingImports) 无法解决导入“pytube” pylance (repotMissingImport) - Import “pytube” could not be resolved pylance (repotMissingImport) 未访问“pytest”/无法解析导入“pytest” - Pylance - "pytest" is not accessed / Import "pytest" could not be resolved - Pylance 无法从 sourcePylancereportMissingModuleSource 解析导入“flask” - Import "flask" could not be resolved from sourcePylancereportMissingModuleSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM