如何将 Pylint 或其他 linters 与 Jupyter 笔记本一起使用?

How to use Pylint or other linters with Jupyter Notebooks?

是否可以在 Jupyter Notebook 中使用 linter?

  • 有可能
  • 您可以为 Jupyter Notebook 安装 pycodestyle,这与 pylint 类似。 您可以在 Jupyter Notebook shell:
  • 中使用以下命令
# install
!pip install pycodestyle pycodestyle_magic


# load
%load_ext pycodestyle_magic


# use
%%pycodestyle
def square_of_number(
     num1, num2, num3, 
     num4):
    return num1**2, num2**2, num3*

# Output
2:1: E302 expected 2 blank lines, found 0
3:23: W291 trailing whitespace

  • 查看 问题了解更多详情。我的回答是受 link.
  • 中答案的启发
  • 查看 this 问题以获得更多类似的 linter 和解决方案。

您可以使用FlakeHell to run any number of linters supported by flake8 on entire notebooks

是 - 您可以使用 nbQA

在 Jupyter 笔记本上 运行 任何标准 Python 代码质量工具

例如:

pip install -U nbqa pylint
nbqa pylint notebook.ipynb

免责声明:我是 nbQA 的作者

如果您想在 Jupyter 中使用 black linter:

pip install black "black[jupyter]"
black {source_file_or_directory}

如果您想使用预提交挂钩自动检查笔记本,则必须将 id: black 替换为 id: black-jupyter (more info here)。