22

Is it possible to have a linter inside of a Jupyter Notebook?

0

4 Answers 4

12
  • Yes it is possible
  • You can install pycodestyle for Jupyter Notebook which is similar to pylint. You can use the below commands from inside a 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

  • Check this question out for more details. My answer is inspired by the answers in the link.
  • Check out this question for more similar linters and solutions.
1
  • This code is not maintained anymore. It is now raising errors that haven't been solved.
    – RomaneG
    May 23, 2022 at 17:36
3

Yes - you can run any standard Python code quality tool on a Jupyter Notebook using nbQA

e.g.:

pip install -U nbqa pylint
nbqa pylint notebook.ipynb

disclaimer: I"m the author of nbQA

4
  • It is good to point out that nbqa has typing-extensions as a dependency. Jan 8 at 3:18
  • no it doesn't, it only requires ipython, tomli, and tokenize-rt. anything else (like typing-extensions) must have been brought in by some other tool Jan 8 at 9:43
  • Maybe pylint requires? I just run that pip install -U nbqa pylint snipped, then pip warned me to install typing-extensions Jan 8 at 16:04
  • yup, pylint requires it :) Jan 8 at 19:17
0

You can use FlakeHell to run any number of linters supported by flake8 on entire notebooks

0

If you want to use the black linter in Jupyter:

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).

4
  • I think you misunderstood the difference between a linter and a formatter. "Black" is not a linter, it's a formatter.
    – S.B
    Jul 16, 2022 at 18:27
  • @S.B I know the difference. black can do either.
    – crypdick
    Jul 18, 2022 at 10:14
  • black.readthedocs.io/en/stable/…
    – S.B
    Jul 18, 2022 at 10:17
  • I stand corrected @S.B , I did misunderstand!
    – crypdick
    Jul 18, 2022 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.