在 Ipython(Jupyter-Notebook)中使用 Pylint
Using Pylint in Ipython (Jupyter-Notebook)
我想在使用 Jupyter-Notebook 时 运行 Pylint 或任何等效程序。有没有办法以这种方式安装和 运行 Pylint?
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
更具体ci回答关于pylint
的问题。在开发/ci 环境(即命令行)中实现此目的的一种相对简单的方法是将笔记本转换为 Python,然后 运行 linting。
假设您在 ./notebooks
文件夹中有笔记本,并且路径中有 jupyter
和 pylint
命令,您可以 运行 以下内容:
jupyter nbconvert \
--to=script \
--output-dir=/tmp/converted-notebooks/ \
./notebooks/*.ipynb
pylint /tmp/converted-notebooks/*.py
您可能需要配置 pylint,因为笔记本样式与一般 Python 模块略有不同。
您可能想要禁用的一些规则:
- 毫无意义的陈述
- 表达式未赋值
- 尾随换行符
- 导入位置错误
- 重新定义外部名称
- 无效名称
单元格中的最大字符数(水平滚动之前)似乎也是 116
,但这可能取决于其他因素。
(例如,可以使用 --max-line-length
和 --disable
pylint 参数或通过 .pylintrc
文件配置这些选项)
我建议您考虑 nbQA 工具:
pip install nbqa pylint
nbqa pylint my_notebook.ipynb
除了 pylint
,nbqa
还可以轻松 运行 其他几个格式化程序和 linter 工具,并且可以通过它们专用的 pre-commit hooks 轻松集成到您的开发工作流程中。
JupyterLab 扩展 jupyterlab-lsp 支持 pylint(但默认禁用 pylint):
https://github.com/python-lsp/python-lsp-server/issues/171#event-6236223765
pip install jupyterlab-lsp
pip install 'python-lsp-server[all]'
这是我在设置 => 高级设置编辑器下的 LanguageServer 选项卡的配置,启用 pylint:
{
"language_servers": {
"pyright": {
"serverSettings": {
"python.analysis.useLibraryCodeForTypes": true
}
},
"pylsp": {
"serverSettings": {
"pylsp": {
"plugins": {
"pydocstyle": {
"enabled": true
},
"pyflakes": {
"enabled": false
},
"flake8": {
"enabled": true
},
"pylint": {
"enabled": true
}
}
}
}
}
}
}
我想在使用 Jupyter-Notebook 时 运行 Pylint 或任何等效程序。有没有办法以这种方式安装和 运行 Pylint?
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
更具体ci回答关于pylint
的问题。在开发/ci 环境(即命令行)中实现此目的的一种相对简单的方法是将笔记本转换为 Python,然后 运行 linting。
假设您在 ./notebooks
文件夹中有笔记本,并且路径中有 jupyter
和 pylint
命令,您可以 运行 以下内容:
jupyter nbconvert \
--to=script \
--output-dir=/tmp/converted-notebooks/ \
./notebooks/*.ipynb
pylint /tmp/converted-notebooks/*.py
您可能需要配置 pylint,因为笔记本样式与一般 Python 模块略有不同。
您可能想要禁用的一些规则:
- 毫无意义的陈述
- 表达式未赋值
- 尾随换行符
- 导入位置错误
- 重新定义外部名称
- 无效名称
单元格中的最大字符数(水平滚动之前)似乎也是 116
,但这可能取决于其他因素。
(例如,可以使用 --max-line-length
和 --disable
pylint 参数或通过 .pylintrc
文件配置这些选项)
我建议您考虑 nbQA 工具:
pip install nbqa pylint
nbqa pylint my_notebook.ipynb
除了 pylint
,nbqa
还可以轻松 运行 其他几个格式化程序和 linter 工具,并且可以通过它们专用的 pre-commit hooks 轻松集成到您的开发工作流程中。
JupyterLab 扩展 jupyterlab-lsp 支持 pylint(但默认禁用 pylint):
https://github.com/python-lsp/python-lsp-server/issues/171#event-6236223765
pip install jupyterlab-lsp
pip install 'python-lsp-server[all]'
这是我在设置 => 高级设置编辑器下的 LanguageServer 选项卡的配置,启用 pylint:
{
"language_servers": {
"pyright": {
"serverSettings": {
"python.analysis.useLibraryCodeForTypes": true
}
},
"pylsp": {
"serverSettings": {
"pylsp": {
"plugins": {
"pydocstyle": {
"enabled": true
},
"pyflakes": {
"enabled": false
},
"flake8": {
"enabled": true
},
"pylint": {
"enabled": true
}
}
}
}
}
}
}