我使用下面的设置

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设置中设置它,但它也没有显示。

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


当前回答

我的解决方案

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

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.

其他回答

这对我来说很管用:

打开命令面板(Ctrl + Shift + P)并选择“Python: Select Interpreter”。

这样做,你在Visual Studio Code中设置Python解释器。

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

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

如果你像我一样更喜欢视觉,你可以使用菜单File→Preferences→Settings (Ctrl +,)中的visual Studio Code配置。选择扩展→Python。

在Analysis: Disabled一节中,添加以下消息的抑制:

我已经通过Ctrl + Shift + P解决了导入错误。 输入“首选项设置”,并选择选项首选项打开设置(JSON)

并添加一行“python”。pythonPath环境”:“/ usr / bin /”

因此JSON内容应该是这样的:

{
    "python.pythonPath": "/usr/bin/"
}

如果存在其他配置行,则保留它们。 这将导入使用PIP自动完成安装的所有模块。

我正在使用以下设置:(在2021年4月)

Macos big sur vscode 水蟒3(环保版)

我在启动Django时遇到了这个错误。 因此,我执行这些步骤,这个错误就得到了解决。

步骤如下截图所示:

打开设置(工作区) 按照这张截图打开Python Path 现在,单击settings.json中的Edit 创建如下截图所示的路径/opt/anaconda3/bin/python

5. 现在,保存这个设置。json文件。 6. 重新启动vscode

此外,智能感知可能不会工作一段时间,保持等待一段时间,然后重新启动,然后vscode读取文件的新路径。