【发布时间】:2018-10-25 18:24:36
【问题描述】:
我想在使用 Jupyter-Notebook 时运行 Pylint 或任何等效设备。有没有办法以这种方式安装和运行 Pylint?
【问题讨论】:
标签: python python-3.x jupyter-notebook pylint flake8
我想在使用 Jupyter-Notebook 时运行 Pylint 或任何等效设备。有没有办法以这种方式安装和运行 Pylint?
【问题讨论】:
标签: python python-3.x jupyter-notebook pylint flake8
pycodestyle 相当于 Jupyter Notebook 的 pylint,它能够根据 PEP8 样式指南检查您的代码。
首先,您需要通过键入此命令在jupyter notebook 中安装pycodestyle,
!pip install pycodestyle pycodestyle_magic
在 jupyter notebook 的单元格中运行此命令。 安装成功后,你必须像这样在 Jupyter Notebook 单元中加载魔法,
%load_ext pycodestyle_magic
然后,您必须在要根据PEP8 标准调查您的代码的单元格中使用pycodestyle。
以下是一些示例,以便更清晰地理解,
%%pycodestyle
a=1
输出:pycodestyle 会给你这个消息,
2:2: E225 missing whitespace around operator
另一个例子,
%%pycodestyle
def square_of_number(
num1, num2, num3,
num4):
return num1**2, num2**2, num3**2, num4**2
输出:
2:1: E302 expected 2 blank lines, found 0
3:23: W291 trailing whitespace
【讨论】:
pycodestyle_magic 似乎不再维护,最后一次提交是从 2019 年开始
我建议你考虑使用nbQA 工具:
pip install nbqa pylint
nbqa pylint my_notebook.ipynb
除了pylint,nbqa 还可以轻松运行其他几种格式化程序和 linter 工具,并通过其专用的pre-commit hooks 轻松集成到您的开发工作流程中。
【讨论】:
更具体地回答有关pylint 的问题。在开发/ci 环境(即命令行)中实现这一目标的一种相对简单的方法是将 notebook 转换为 Python,然后运行 linting。
假设您在./notebooks 文件夹中有笔记本并且路径中有jupyter 和pylint 命令,您可以运行以下命令:
jupyter nbconvert \
--to=script \
--output-dir=/tmp/converted-notebooks/ \
./notebooks/*.ipynb
pylint /tmp/converted-notebooks/*.py
您可能需要配置 pylint,因为 notebook 样式与一般 Python 模块略有不同。
您可能想要禁用的一些规则:
似乎一个单元格中的最大字符数(水平滚动之前)是116,但这可能取决于其他因素。
(例如,可以使用--max-line-length 和--disable pylint 参数或通过.pylintrc 文件配置这些选项)
【讨论】: