如何将 Pylint 或其他 linter 与 Jupyter Notebook 一起使用?

问题描述 投票:0回答:5

Jupyter Notebook 中是否可以有 linter?

python jupyter-notebook pylint linter
5个回答
12
投票
  • 是的,这是可能的
  • 您可以为 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

  • 查看这个问题以了解更多详细信息。我的答案受到链接中答案的启发。
  • 查看this问题以获取更多类似的linter和解决方案。

5
投票

是的 - 您可以使用 nbQA

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

例如:

pip install -U nbqa pylint
nbqa pylint notebook.ipynb

免责声明:我是 nbQA 的作者


0
投票

您可以使用 FlakeHell 在整个笔记本上运行 flake8 支持的任意数量的 linter


0
投票
如果你想在 Jupyter 中使用

black

 linter:

pip install black "black[jupyter]" black {source_file_or_directory}
如果您想使用预提交挂钩自动检查笔记本,则必须将 

id: black

 替换为 
id: black-jupyter
更多信息请参见此处)。


0
投票
如果您正在寻找 JupyterLab 而不是 JupyterNotebook 的解决方案,您可能需要使用支持 pylint 的

jupyterlab-lsp 。另请参阅此处“重复”问题的答案: https://stackoverflow.com/a/71495554/2876079

© www.soinside.com 2019 - 2024. All rights reserved.