我使用下面的设置

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

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


当前回答

如果出现Pylint错误,请安装以下程序

pipenv install pylint-django

然后在根文件夹中创建一个文件.pylintrc,并编写以下内容

load-plugins=pylint-django

其他回答

如果出现Pylint错误,请安装以下程序

pipenv install pylint-django

然后在根文件夹中创建一个文件.pylintrc,并编写以下内容

load-plugins=pylint-django

这个问题已经在GitHub上打开了:

Python无法解决导入问题#3840

MagnuesBrzenk和SpenHouet给出了两个非常有用的答案。

目前最好的解决方案是在项目根文件夹中创建一个.env文件。然后像这样添加一个PYTHONPATH:

PYTHONPATH=YOUR/MODULES/PATH

在你的设置中。json添加:

"python.envFile": ".env"

我的解决方案

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

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中打开visual studio代码设置,并在“[python]”后添加以下参数:

"python.linting.pylintArgs": ["--rep[![enter image description here][1]][1]orts", "12", "--disable", "I0011"],
"python.linting.flake8Args": ["--ignore=E24,W504", "--verbose"]
"python.linting.pydocstyleArgs": ["--ignore=D400", "--ignore=D4"]

这帮助我避免了VSCode中的pylint警告。

对我来说,如果我将python、pylint和autopep8的路径设置为本地环境路径,它就可以工作。

在你的工作区中添加/修改如下内容:

"python.pythonPath": "...\\your_path\\.venv\\Scripts\\python.exe",
"python.linting.pylintPath": "...\\your_path\\.venv\\Scripts\\pylint.exe",
"python.formatting.autopep8Path": "...\\your_path\\.venv\\Scripts\\autopep8.exe",

保存并使用工作区重新启动VS Code。 完成了!