简体   繁体   English

无法从源 Pylance (reportMissingModuleSource) 解析导入“flask”

[英]Import "flask" could not be resolved from source Pylance (reportMissingModuleSource)

When I am writing当我在写作

from flask import Flask

One Yellow line is coming up under flask and stating Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) .一条黄线出现在flask下,并说明Import "flask" could not be resolve from source Pylance (reportMissingModuleSource) Also, I am able to do the work with this package also successfully.此外,我也能够成功地使用这个 package 完成工作。 But the thing is, I am not able to use autosuggesstions for Classes and methods very well.但问题是,我不能很好地为类和方法使用自动建议。

Further:更远:

  1. I have checked that flask is installed successfully.我检查了 flask 安装成功。
  2. Also I visited this thread https://github.com/microsoft/pylance-release/issues/236我也访问了这个线程https://github.com/microsoft/pylance-release/issues/236

And I set up my settings.json in vscode as follows:我在 vscode 中设置了我的settings.json如下:

"python.analysis.extraPaths": [
    "/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]

But Still, unable to resolve the error.但仍然无法解决错误。

When I did not install the module "flask" in the Python environment currently used in VSCode:当我没有在VSCode当前使用的Python环境中安装“flask”模块时:

在此处输入图像描述

Please use the command " pip --version " to check the source of the module installation tool "pip", the module is installed at this location:请使用命令“ pip --version ”查看模块安装工具“pip”的来源,模块安装在这个位置:

在此处输入图像描述

Then, we can use the command " pip show flask " to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)然后,我们可以使用命令“ pip show flask ”来检查模块“flask”的安装位置:(检查模块的安装位置是否与显示在左下角VSCode的ZA7F5F35426B9237411FC9231B环境中的ZA7F5F35426B9237411FC9231B环境一致)。

在此处输入图像描述

If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.如果此处仍然显示“reportMissingModuleSource”消息,请重新加载 VS Code。

( F1 , Developer: Reload Window ) ( F1 , Developer: Reload Window )

在此处输入图像描述

Are you using a Virtualenv?您使用的是 Virtualenv 吗? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.如果是这样,请确保 VSCode 使用 virtualenv 作为您的 python 解释器,否则它将无法获取您安装在此 virtualenv 中的包。

To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.为此,请单击底部栏中的 Python 解释器,您应该获得可能的 python 解释器列表,包括您的 virtualenv。

I had a similar issue while trying to import flask on vscode.我在 vscode 上尝试导入 flask 时遇到了类似的问题。 I fixed it by using anaconda .我使用anaconda修复了它。 Simply you install the flask module in your created environment example screenshot .只需在您创建的环境示例屏幕截图中安装 flask 模块即可。

How to create a virtual env in anaconda:如何在 anaconda 中创建虚拟环境:
1. On the left sidebar, click on environments. 1. 在左侧边栏上,单击环境。
2. Click create (at the bottom). 2. 单击创建(在底部)。
3. At the pop-up window, give your vir.env a name and select the language version. 3. 在弹出窗口 window 中,为您的 vir.env 命名,并为 select 命名语言版本。
4. Once created, you can start installing different modules in your environment. 4. 创建后,您可以开始在您的环境中安装不同的模块。

I hope that helps!我希望这会有所帮助!

For Linux Mint and for those who have installed flask, but VSCode doesn't find it:对于 Linux Mint 和已安装 flask 但 VSCode 没有找到它的用户:

  1. check the Flask path: pip show flask (should be smth like Location: /home/<username>/.local/lib/python3.8/site-packages检查 Flask 路径: pip show flask (应该像Location: /home/<username>/.local/lib/python3.8/site-packages
  2. in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path .在 VSCode 单击左下角按钮并选择 python 解释器,在我的情况下,我将其从python3.9更改为python3.8 ,我们可以在 flask path中看到它。

That's because you have not chosen your path correctly, type:那是因为您没有正确选择路径,请键入:
pipenv --venv
then it will show you where your virtual env is installed.然后它将显示您的虚拟环境的安装位置。 Check where the packages are installed in your env, and then type what comes to you from the shell\scripts or whatever\python, and the packages will work.检查包在你的环境中的安装位置,然后输入你从 shell\scripts 或任何\python 中得到的内容,这些包就可以工作了。

I was facing same issue.我面临同样的问题。 I tried all solutions from stack-overflow but none worked.我尝试了堆栈溢出的所有解决方案,但没有一个有效。 But after lot of searching and time waste I found my silly mistake.但是经过大量搜索和时间浪费后,我发现了我的愚蠢错误。 I had created folder named 'flask' and stored my project there.我创建了名为“flask”的文件夹并将我的项目存储在那里。 I'm beginner and going through lot of such silly mistakes.我是初学者,经历了很多这样愚蠢的错误。 Hope it would help, if somebody commits same mistake.希望它会有所帮助,如果有人犯了同样的错误。

By the following steps, I solved this issue:通过以下步骤,我解决了这个问题:

1)On the project directory create the .flaskenv file 1)在项目目录中创建.flaskenv文件

  1. In the .flaskenv file write the following two lines:.flaskenv文件中写入以下两行:

FLASK_ENV=development FLASK_ENV=发展

FLASK_APP=main.py FLASK_APP=main.py

Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.请注意 main.py 是我写完flask run后的主文件,另外,您必须先创建一个 env 文件夹或需求文件。

I solved my problem by using a "Global" version of Python.我通过使用 Python 的“全球”版本解决了我的问题。 Maybe Pylance had not updated to work with the version of Python I was using.也许 Pylance 没有更新到我正在使用的 Python 版本。 interpreter language screenshot解释器语言截图

A few answers ( Jill 's, Marius 's, and Roy 's) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly.一些答案( Jill的、 Marius的和Roy的)提到了选择正确的 Python 解释器以正确制作 Pylance function 的事实。 I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.我想补充一点,当使用Jupyter Notebook并选择了正确的 Python kernel 时,仍然需要这样做。

It is counterintuitive to choose both Python interpreter and notebook's Python kernel to make things work.选择 Python 解释器和笔记本的 Python kernel 来使事情正常工作是违反直觉的。 It is even more counterintuitive considering the fact that Python interpreter's button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open.考虑到 Python 解释器的按钮(在屏幕左下角,在状态栏上)在 Jupyter Notebook 打开时不一定会出现,但当 Python 脚本打开时,这更加违反直觉。 For instance, in this screenshot , we see the little line under Scikit-learn's import, indicating a problem with the import (even though the import is successful).例如,在这个屏幕截图中,我们看到 Scikit-learn 的导入下方的小线,表示导入有问题(即使导入成功)。 However, the correct Python kernel, with Scikit-learn installed, is already chosen.但是,已选择安装了 Scikit-learn 的正确 Python kernel。 Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn.仅打开 Python 脚本,我们注意到 Python 解释器是这种行为的原因,因为选择了错误的解释器,没有 Scikit-learn。 In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab).从某种意义上说,可以认为是 Python kernel 或 Conda 环境出现问题(使用 Jupyter Notebook 和 Jupyter Lab 进行实验时经常遇到此类问题)。 I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code.我希望这个答案可以帮助那些在 VS Code 中的 Jupyter Notebooks 的特定上下文中寻找解决这个问题的人。 They could ignore the other answers because they could think it is not the case for them.他们可能会忽略其他答案,因为他们可能认为情况并非如此。

I think this is a Vscode problem,just restart the Vscode.我认为这是一个 Vscode 问题,只需重新启动 Vscode。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM