Using Pylint in Ipython (Jupyter-Notebook)
Asked Answered
C

5

27

I want to run Pylint or any equivalent while using Jupyter-Notebook. Is there a way to install and run Pylint this way?

Chelseychelsie answered 15/5, 2018 at 20:0 Comment(1)
refer to this link : #26127353Leslie
H
21

pycodestyle is an equivalent of pylint for Jupyter Notebook which is able to check your code against the PEP8 style guide.

First, you need to install the pycodestyle in jupyter notebook by typing this command,

!pip install pycodestyle pycodestyle_magic

Run this command in a cell of jupyter notebook. After successful installation, you have to load the magic in a Jupyter Notebook cell like this,

%load_ext pycodestyle_magic

Then, you have to use pycodestyle in a cell in which you want to investigate your code against PEP8 standards.

Below are some examples for more and clear understanding,

%%pycodestyle
a=1

Output: pycodestyle will give you this message,

2:2: E225 missing whitespace around operator

Another example,

%%pycodestyle
def square_of_number(
     num1, num2, num3, 
     num4):
    return num1**2, num2**2, num3**2, num4**2

Output:

2:1: E302 expected 2 blank lines, found 0
3:23: W291 trailing whitespace
Houseroom answered 20/1, 2019 at 16:55 Comment(3)
Pylint does a lot more than just check the style, a lot of people would want to use Pylint for it's static analysis error detection capabilities, can this library do that as well?Dominoes
@shawn, I didn't upvote this answer and didn't award the bounty I placed because it doesn't actually answer the question regarding Pylint.Dominoes
unfortunately, it looks like pycodestyle_magic is no longer maintained, the last commit is from 2019Gaither
D
15

I suggest you consider the nbQA tool:

pip install nbqa pylint
nbqa pylint my_notebook.ipynb

Besides pylint, nbqa makes it easy to run several other formatter and linter tools and is easy to integrate into your development workflow via their dedicated pre-commit hooks.

Discernment answered 3/3, 2021 at 14:40 Comment(0)
P
5

To answer the question more specifically in regards to pylint. One relatively simple way to achieve that in a development / ci environment (i.e. command line) is to convert the notebook to Python and then run the linting.

Let's assume you have notebooks in the ./notebooks folder and you have the jupyter and pylint command in the path, you could run the following:

jupyter nbconvert \
    --to=script \
    --output-dir=/tmp/converted-notebooks/ \
    ./notebooks/*.ipynb
pylint /tmp/converted-notebooks/*.py

You might want to configure pylint, as the notebook style is slightly different to a general Python module.

Some rules that you might want to disable:

  • pointless-statement
  • expression-not-assigned
  • trailing-newlines
  • wrong-import-position
  • redefined-outer-name
  • invalid-name

It also appears that the maximum number of characters in a cell (before horizontal scrolling) is 116 but that might depend on other factors.

(These options can for example be configured using the --max-line-length and --disable pylint arguments, or via the .pylintrc file)

Precedence answered 24/6, 2020 at 14:49 Comment(0)
P
4

The JupyterLab extension jupyterlab-lsp supports pylint (but pylint is disabled by default):

https://github.com/python-lsp/python-lsp-server/issues/171#event-6236223765

pip install jupyterlab-lsp
pip install 'python-lsp-server[all]'

Here is my config for the LanguageServer tab under Settings => Advanced Settings Editor, enabling 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
                        }
                    }
                }
            }
        }
    }
    
}
Pucker answered 16/3, 2022 at 10:36 Comment(2)
For updated configuration see related question https://mcmap.net/q/534374/-jupyterlab-3-0-14-how-to-disable-code-style-highlights-pycodestylePucker
Did you ever figure out how to reformat a notebook using black or yapf using Jupyter-lsp?Dogmatize
P
0

In case you opt for using nbqa but you happen to have notebooks using pyspark as framework (instead of python), the following command will fail to recognize your notebooks:

nbqa pylint **/*.ipynb

The reason is that .ipynb files using pyspark have this metadata in the body:

"metadata": {
   "name": "pyspark"
  }

which prevents nbqa for recognizing it as python type. The solution is to simply substitute the value of "pyspark" with "python", here is a shell script that does it (you might need to install jq):

for i in $(find . -type f -name "*.ipynb");
  do jq '.metadata.language_info.name="python"' "$i" > "$i".ipynb;
  nbqa pylint "$i".ipynb -f colorized --fail-under=7;
  rm "$i".ipynb;
done;

Pinion answered 1/11, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.