简体   繁体   English

如何在 Jupyter Notebooks 中使用 Pylint 或其他 linter?

[英]How to use Pylint or other linters with Jupyter Notebooks?

Is it possible to have a linter inside of a Jupyter Notebook?是否有可能在 Jupyter Notebook 内放置一个 linter?

  1. Yes it is possible对的,这是可能的
  2. You can install pycodestyle for Jupyter Notebook which is similar to pylint .您可以为pycodestyle Notebook 安装类似于 pylint 的pylint You can use the below commands from inside a Jupyter Notebook shell:您可以在 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

  1. Check this question out for more details.查看问题以获取更多详细信息。 My answer is a short form of the answer to the question I provided in the link.我的回答是对我在链接中提供的问题的简短回答。
  2. Check out this question for more similar linters and solutions.查看问题以获取更多类似的短绒和解决方案。

You can use FlakeHell to run any number of linters supported by flake8 on entire notebooks您可以使用FlakeHell在整个笔记本上运行 flake8 支持的任意数量的 linter

Yes - you can run any standard Python code quality tool on a Jupyter Notebook using nbQA是 - 您可以使用nbQA在 Jupyter Notebook 上运行任何标准 Python 代码质量工具

eg:例如:

pip install -U nbqa pylint
nbqa pylint notebook.ipynb

disclaimer: I"m the author of nbQA免责声明:我是 nbQA 的作者

If you want to use the black linter in Jupyter:如果你想在 Jupyter 中使用black短绒:

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

If you want to auto-lint your notebooks with a pre-commit hook, you have to replace id: black with id: black-jupyter ( more info here ).如果你想用 pre-commit 钩子自动 lint 你的笔记本,你必须用id: black-jupyter替换id: black更多信息在这里)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM