Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pylint in Ipython (Jupyter-Notebook)

Tags:

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

like image 585
mccurcio Avatar asked May 15 '18 20:05

mccurcio


People also ask

Can you use Pylint in Jupyter Notebook?

NbQa is a tool that allows you to run any standard python linter on a Jupyter notebook. The following linters are currently accessible from this tool: pylint, black, auto flake, check-ast, doctest, flake8, mypy and yapf.

Is IPython deprecated?

By default, this was Python. The IPython console is now deprecated and if you want to start it, you'll need to use the Jupyter Console, which is a terminal-based console frontend for Jupyter kernels.

How do I enable autocomplete in JupyterLab?

To enable code autocomplete in Jupyter Notebook or JupyterLab, you just need to hit the Tab key while writing code. Jupyter will suggest a few completion options. Navigate to the one you want with the arrow keys, and hit Enter to choose the suggestion.

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


3 Answers

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
like image 161
Abdur Rehman Avatar answered Sep 22 '22 23:09

Abdur Rehman


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.

like image 10
leopold.talirz Avatar answered Sep 25 '22 23:09

leopold.talirz


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)

like image 5
de1 Avatar answered Sep 25 '22 23:09

de1