36

My understanding is that the latest release of Pylint (1.0.0 at the time of this writing) has support for Python 3, but I can't get it to work on 64-bit Ubuntu 13.04 with Python 3.3.

I followed the installation instructions on the PyPi site, and Pylint 1.0.0 seems to be installed successfully (pylint --version returns pylint 1.0.0), and works with Python 2.7 code, but it reports a syntax error when it sees nonlocal statements and such.

What gives? Are there special installation instructions for Pylint on Ubuntu?

1
  • pylint versions before 2.0.0 or so have separate executables for Python2 vs Python3 code. pylint is for Python2 and pylint3 is for Python3. As of version 2.0.0 or so, we are back to one executable named pylint, and it handles only Python3 code. To get this latest version from the GitHub releases page, see my answer I just added here. Commented Mar 13, 2022 at 1:52

7 Answers 7

30

Python 2 and 3 are separate beasts. If you install a script into the site-packages of one version, you are not installing it into the other.

I'd install it through pip, but you'll need the right version of pip.

sudo apt-get install python3-pip
sudo pip-3.3 install pylint

This will replace your 2.7 version. We can confirm this by checking less $(which pylint):

#!/usr/bin/python3.3
# EASY-INSTALL-ENTRY-SCRIPT: 'pylint==1.0.0','console_scripts','pylint'
__requires__ = 'pylint==1.0.0'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pylint==1.0.0', 'console_scripts', 'pylint')()
    )
3
  • Great. That works. I'll note, however, that those commands did not replace the 2.7 version on my machine. I had to remove it from the 2.7 dist-packages, and then rerun the commands. Commented Sep 3, 2013 at 17:12
  • 3
    @Oli so if I install pylint for python 3 then the default python 2 version doesn't work?
    – sayth
    Commented Sep 8, 2013 at 3:07
  • 7
    I found out that with newer versions (as of Python 3.5 etc. Nov 2016) there is also a pylint3 which you can install with: sudo apt install pylint3. Commented Nov 27, 2016 at 2:32
22

@sayth 's comment to the accepted answer was what drew me here -- I write both python 2 and python 3 scripts, and I want to be able to check either against the correct ruleset. installing pylint using pip3 install pylint writes a short script to /usr/local/bin which invokes the python3 interpreter, and seems, therefore to assume all files to be checked are python 3 scripts.

to work around this, I now have the following files:

~/bin/pylint2:

#!/usr/bin/python2
# EASY-INSTALL-ENTRY-SCRIPT: 'pylint','console_scripts','pylint'
__requires__ = 'pylint'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pylint', 'console_scripts', 'pylint')()
    )

and ~/bin/pylint3:

#!/usr/bin/python3
# EASY-INSTALL-ENTRY-SCRIPT: 'pylint','console_scripts','pylint'
__requires__ = 'pylint'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pylint', 'console_scripts', 'pylint')()
    )

and then, because I like to use pylint directly from Geany's "Build Commands" menu, and I can't specify different commands for python 2 and python 3 scripts, i also have ~/bin/pylint:

#!/bin/bash
if [[ $(head -n 1 "${@: -1}") == *python3* ]]
then
    # python3 file
    pylint3 "$@"
else
    pylint2 "$@"
fi

which dispatches the correct version by sniffing the shebang.

Not perfect, certainly, but functional and, perhaps, useful for others.

18

The pylint ecosystem has changed since (after this question was asked), and there is now a separate pylint for python3. It can be installed with:

sudo apt install pylint3

Worked for me on Ubuntu 16.04.2 LTS

3
  • 1
    this works for me as well. Installed pylint for python3, and still keep pylint for python 2.
    – zhihong
    Commented Sep 12, 2017 at 15:24
  • This answer is the correct one for system-wide installation.
    – jII
    Commented Apr 30, 2019 at 18:12
  • Note that if you use a system-wide installation but then want to analyse a project inside a virtualenv, it won't find your imports. See for instance github.com/Microsoft/vscode-python/issues/1185 "This is a common error that most people come across. You have not installed pylint in the same environment that contains your python packages." Commented Apr 8, 2020 at 5:54
8

As another method for running pylint on both Python 2 and 3, note that you can use Python's -m switch to run a module installed on the system in the current version of Python, so you can do

$ python2 -m pylint
$ python3 -m pylint

to explicitly select which one you want. You could make these into aliases or shell scripts if you wanted.

2

The root of the problem is that pylint should come with entry point console scripts for /usr/local/bin/pylint2 and /usr/local/bin/pylint3. This should be considered a bug.

The following does not work; it still runs pylint2:

python3 -m pylint p3file.py

The following is what I have been using successfully:

python2 /usr/local/bin/pylint p2file.py
python3 /usr/local/bin/pylint p3file.py
1

For the latest versions of pylint, tested in Aug. 2023

Tested on Ubuntu 22.04.

New instructions:

See: https://github.com/PyCQA/pylint#install

pip3 install pylint
# upgrade to the latest version; see:
# https://stackoverflow.com/a/47071257/4561887
pip3 install pylint --upgrade

Old instructions are below this point:


This answer completely rewritten since the downvote. Tested in Ubuntu 18.04.

pylint versions before 2.0.0 or so have separate executables for Python2 vs Python3 code. pylint is for Python2 and pylint3 is for Python3. As of version 2.0.0 or so, we are back to one executable named pylint, and it handles only Python3 code. To get this latest version from the GitHub releases page, see Option 1 below.

Summary

Option 1 is recommended. Option 2 works, but is obsolete.

  1. Option 1: See "Option 1 Installation steps" below. To get the latest version of pylint for Python3, you must manually download the latest release from GitHub, extract it, and then run this to install it:
    # Install it
    sudo python3 setup.py install
    # Check its version
    pylint --version
    
    The version will be pylint 2.12.2 or later and the executable for Python3 will be called pylint. There is no executable for Python2, since Python2 is obsolete and has been "sunset", or deprecated, since 1 Jan. 2020.
    This version of pylint shows code numbers for problems it finds in your code, such as C0301 or W0105, as shown below.
  2. Option 2: See "Option 2 Installation steps" below.
    1. To get an older version of pylint, such as pylint 1.9.2, which works only on Python2 code, do this:
      # Install it
      sudo apt update
      sudo apt install pylint 
      # Check its version
      pylint --version
      
    2. To get an older version of pylint3, such as pylint3 1.8.3, which works only on Python3 code, do this:
      # Install it
      sudo apt update
      sudo apt install pylint3
      # Check its version
      pylint3 --version
      
    3. These older versions have separate executables for Python2 and Python3 code, and they do NOT show code numbers for problems they find in your code. For these older versions, use pylint for Python2 code and pylint3 for Python3 code.

Details

Resources:

  1. GitHub source code: https://github.com/PyCQA/pylint
  2. Pylint User Manual: https://pylint.pycqa.org/en/latest/
    1. Tutorial: https://pylint.pycqa.org/en/latest/tutorial.html. Notice that the pylint output shows these 5 types of messages (emphasis added):

      Output:
      Using the default text output, the message format is :
      MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
      There are 5 kind of message types :

      • (C) convention, for programming standard violation
      • (R) refactor, for bad code smell
      • (W) warning, for python specific problems
      • (E) error, for probable bugs in the code
      • (F) fatal, if an error occurred which prevented pylint from doing further processing.

[RECOMMENDED] Option 1: Install the latest version of pylint (version 2.12.2 or later; from the GitHub repo) for Python3 on Ubuntu

This should work on any version of Ubuntu I believe. Tested on Ubuntu 18.04. For best results and to get the latest version of pylint for Python3, these are the installation steps I recommend you follow.

Sample pylint --version information (for Python3) after following these steps:

pylint 2.12.2
astroid 2.9.3
Python 3.6.9 (default, Dec  8 2021, 21:08:43) 
[GCC 8.4.0]

Sample usage command and output. Notice it does show the Convention and Warning codes, which is great!:

eRCaGuy_hello_world/python$ pylint hello_world.py  
************* Module hello_world  
hello_world.py:4:0: C0301: Line too long (102/100) (line-too-long)
hello_world.py:33:0: C0116: Missing function or method docstring (missing-function-docstring)
hello_world.py:49:-1: W0105: String statement has no effect (pointless-string-statement)

-----------------------------------
Your code has been rated at 4.00/10

Option 1 Installation steps:

  1. Go to the "Releases" page for pylint on GitHub, and download the source code for the latest release (ex: v2.12.2).
  2. In your file manager, right-click on the downloaded file and go to "Extract Here". This will produce a directory named pylint-2.12.2, for instance.
  3. cd into the extracted directory and run the setup.py script; ex:
    cd path/to/pylint-2.12.2
    sudo python3 setup.py install
    
    See also: https://github.com/PyCQA/pylint#install
  4. Check your version and ensure it now says pylint 2.12.2
    pylint --version
    
  5. Lint some code:
    pylint path/to/my_python3_file.py
    

pylint is a Python file itself. which pylint shows it is located in /home/gabriel/.local/bin/pylint. Running cat "$(which pylint)" shows what it contains:

$ cat "$(which pylint)"
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pylint import run_pylint
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(run_pylint())

Option 2: Install an obsolete version of pylint (from the Ubuntu apt repositories) for Python2 and Python3 on Ubuntu

This should work on Ubuntu 16.04 or later, I believe. Tested on Ubuntu 18.04.

Sample pylint --version information (for Python2 only) after following these steps:

No config file found, using default configuration
pylint 1.9.2, 
astroid 1.6.6
Python 2.7.17 (default, Feb 27 2021, 15:10:58) 
[GCC 7.5.0]

Sample pylint3 --version information (for Python3 only) after following these steps:

No config file found, using default configuration
pylint3 1.8.3, 
astroid 1.6.0
Python 3.6.9 (default, Dec  8 2021, 21:08:43) 
[GCC 8.4.0]

Sample usage command and output. Notice it does NOT show the Convention and Warning codes, because this version is obsolete!:

eRCaGuy_hello_world/python$ pylint3 hello_world.py
No config file found, using default configuration
************* Module hello_world
C:  4, 0: Line too long (102/100) (line-too-long)
C: 33, 0: Missing function docstring (missing-docstring)
W: 49,-1: String statement has no effect (pointless-string-statement)

Option 2 Installation steps:

# Update apt repositories
sudo apt update

# Install `pylint` for Python2
sudo apt install pylint

# Install `pylint3` for Python3
sudo apt install pylint3

Now you can use them to check your Python2 and Python3 files:

# python2
pylint path/to/my_python2_file.py

# python3
pylint3 path/to/my_python3_file.py

The pylint and pylint3 executables are actually Python scripts themselves. Check out their executable paths and what they contain:

eRCaGuy_hello_world/python$ which pylint
/usr/local/bin/pylint
eRCaGuy_hello_world/python$ which pylint3
/usr/bin/pylint3
eRCaGuy_hello_world/python$ cat "$(which pylint)"
#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pylint import run_pylint

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(run_pylint())
eRCaGuy_hello_world/python$ cat "$(which pylint3)"
#! /usr/bin/python3
# EASY-INSTALL-ENTRY-SCRIPT: 'pylint==1.8.3','console_scripts','pylint'
__requires__ = 'pylint==1.8.3'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('pylint==1.8.3', 'console_scripts', 'pylint')()
    )
4
  • Regarding the recommended option 1. Have you tried installing the latest version of pylint from GitHub using pipx (sudo apt install pipx) in a Python virtual environment? That way you get full access to pylint from the default desktop environment without using sudo to install it. It's got the same advantages as the recommended installation method, but it's possibly more secure.
    – karel
    Commented Aug 8, 2023 at 5:15
  • @karel, no, I've never used pipx. I did just run pip3 install pylint and then pip3 install pylint --upgrade on Ubuntu 22.04, however, without sudo, and it worked fine. Some major installation changes have happened to pylint in the last year or so, I think, as I used to have to manually run the installation script with sudo python3 setup.py install, but that script no longer exists. It's been removed from the repo. Commented Aug 8, 2023 at 5:19
  • I read a command cd path/to/pylint-2.12.2 && sudo python3 setup.py install in the option 1 section, and it occurred to me that maybe the sudo is unnecessary.
    – karel
    Commented Aug 8, 2023 at 5:28
  • @karel, it might be. It's been over a year since I tried it on my old Ubuntu 18.04 computer, and I no longer have that OS. I imagine I would have tried it without sudo first, and then only added sudo once I determined it was necessary, but I can't be sure. I know I've had quite a bit of trouble installing pylint in the past--hence this super long answer. I'm glad pip3 does it easily without sudo now. Commented Aug 8, 2023 at 5:30
0

This is in response to simons fine answer. I just thought about it in a different way and thought it could be useful for those seeking solutions for multiple versions of python/pylint.

Installing pylint for 3.x and keeping 2.7 default or vise versa is easily done using virtualenv.

Create your virtual environment. in your env while activated run

    pip install pylint

here you can then figure out where your env has put your python and pylint by

    which pylint
    #/home/$USER/Desktop/python/awesomeSauce/bin/pylint

and then

    which python
    #/home/$USER/Desktop/python/awesomeSauce/bin/python

Then it's just a matter of setting up your ide to use that linting path and/or python path. I know it works with Sublime3 so Ill use that in the example below.

in Sublime in the top header menu select Preferences > Package Settings > Pylinter > Settings - User.

It's just a json object. Set the

    "python_bin": "python",
    // to the python path found earlier by 'which python'
    "python_bin": "/home/$USER/Desktop/python/awesomeSauce/bin/python",
    // dont for get the coma if it is the last element.
    // We also change the pylint_path from
    "pylint_path": null,
    // to
    "pylint_path": "/home/$USER/Desktop/python/awesomeSauce/bin/pylint",
    // sorry I cant make the formatting look any better.

Save the file. I also make a copy of the file and keep it in that venv directory so I can easily switch by copying and pasting this config when I need this linter. When I don't I just reset the Pylinter.sublime-settings back to the default for user and that seems to be the easiest way I have found. Sorry I dont know the windows commands or I would have put them in there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .