我使用下面的设置

macOS v10.14 (Mojave) Python 3.7.1 Visual Studio Code 1.30 2.2.2 Pylint Django 2.1.4

我想使用linting使我在Visual Studio Code中的工作更容易一些。然而,对于每一个进口,我都有“未解决的进口”的状态。即使是默认的Django导入(即从Django .db导入模型)。

我认为这是因为它没有看到虚拟环境的Python文件。

一切都很好,但它开始变得烦人。

我选择的解释器都是Python的系统版本。它似乎根本看不到我的虚拟环境Python(它与我的工作空间不在同一个目录中,因此这部分是有意义的)。

如果我设置好python。设置中的PythonPath。Json文件,它只是忽略它,没有列出我的虚拟环境路径作为一个选项。我还尝试在我的全局Python设置中设置它,但它也没有显示。

有没有快速修复方法让它工作?


当前回答

我想知道这个问题有多少解决方案(或没有),我尝试了上面的大部分,没有任何工作,唯一有效的解决方案是将python语言服务器设置为Jedi,而不是在设置中设置Microsoft。json文件:

"python.languageServer": "Jedi"

其他回答

我用命令行python解决了这个问题。 我在项目路径中使用vs code终端安装了模块,但当在windows命令行python上导入模块时,会抛出一个错误,因为此模块未定义 所以我从命令行安装这些模块,我的问题解决了。

在你的工作空间设置中,你可以这样设置你的Python路径:

{
    "python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}

如果你的设置中有这个代码。Json文件,删除它:

{
    "python.jediEnabled": false
}

我的解决方案

此解决方案仅适用于当前项目。

In the project root, create folder .vscode Then create the file .vscode/settings.json In the file setting.json, add the line (this is for Python 3) { "python.pythonPath": "/usr/local/bin/python3", } This is the example for Python 2 { "python.pythonPath": "/usr/local/bin/python", } If you don't know where your Python installation is located, just run the command which python or which python3 on the terminal. It will print the Python location. This example works for dockerized Python - Django.

之前的答案对我都没用。将下面两行添加到我的设置中。Json文件,然而。

"python.analysis.disabled": [
    "unresolved-import"
],
"python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"]

第一行实际上只是隐藏了检测错误。当然这不是一个永久的解决方案,但可以让屏幕变得整洁。

这个答案给了我第二行:VS Code PyLint错误E0602(未定义的变量)与ProtoBuf编译的Python结构

也许比我更懂Python的人能再解释一下。