如何在VSCode中使pylint和black一起工作?

时间:2018-11-04 17:30:58

标签: visual-studio-code

我一直试图让pylint和black在VSCode上一起工作而没有成功。

我可以使任何一个工作,但只有当另一个不工作时(即pylint将格式化黑色,但是pylint将不报告任何内容,或者pylint将报告错误,但是运行“格式代码”却没有任何作用)。

这是我的setttings.json:

{
    "editor.formatOnSave": true,
    "editor.formatOnPaste": false,
    "editor.formatOnType": false,
    "python.pythonPath": "/usr/bin/python3",
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "/usr/bin/pylint-3",
    "python.linting.flake8Enabled": false,
    "python.linting.flake8Path": "/usr/bin/flake8-3",
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "/usr/bin/black",
    "python.formatting.blackArgs": [
        "--line-length=110"
    ],
    "python.unitTest.pyTestEnabled": true,
    "workbench.colorTheme": "Default Light+",
    "workbench.settings.editor": "json",
    "python.linting.enabled": true
}

环境:

  • Fedora 29
  • VSCode 1.28.2
  • Python扩展2018.9.2
  • pylint 2.1.1
  • 黑色18.6b4

2 个答案:

答案 0 :(得分:0)

在没有成功之前,我已经尝试过了。如果我的记忆正确,flake8 + autopep8都不会。现在由于某些依赖冲突,我什至无法将它们两个安装在同一pipenv虚拟环境中。如果您有兴趣,可以查看下面的输出。

当前,我使用pylint + autopep8(VS Code默认),如果您想坚持使用black,我想您需要将短绒棉换成其他。< / p>

// I have pylint installed already
➜ pipenv install --dev autopep8 
Installing autopep8…
Adding autopep8 to Pipfile's [dev-packages]…
✔ Installation Succeeded 
Pipfile.lock (3830f9) out of date, updating to (6cacd5)…
Locking [dev-packages] dependencies…
✘ Locking Failed! 
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/resolver.py", line 69, in resolve
[pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 726, in resolve_deps
[pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 395, in resolve
[pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches black
[pipenv.exceptions.ResolutionFailure]:       Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches black
Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0
There are incompatible versions in the resolved dependencies.
[pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 726, in resolve_deps
[pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]:   File "/home/xzhan/miniconda3/lib/python3.7/site-packages/pipenv/utils.py", line 395, in resolve
[pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches black
[pipenv.exceptions.ResolutionFailure]:       Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches black
Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0
There are incompatible versions in the resolved dependencies.

答案 1 :(得分:0)

这对我有用:

我认为,秘密秘诀的一部分是禁用某些与黑色冲突的pylint警告(“ disable = ...”)

在根目录中添加.pylintrc文件:

# Looks like setup.cfg cannot load the extensions in the precommit,
# but that the pylintrc file can
# This happens even when specifying --rcfile=setup.cfg
# Possible bug from pylint?
[MASTER]
load-plugins = pylint.extensions.docparams, pylint.extensions.docstyle, pylint.extensions.mccabe

[BASIC]
accept-no-param-doc = no
accept-no-raise-doc = no
accept-no-return-doc = no
accept-no-yields-doc = no
default-docstring-type = numpy

[FORMAT]
max-line-length = 88

[MESSAGES CONTROL]
disable = C0330, C0326, C0199, C0411