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?
17 Answers
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 pylintThis 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 @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 "$@"
fiwhich dispatches the correct version by sniffing the shebang.
Not perfect, certainly, but functional and, perhaps, useful for others.
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
3As 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 pylintto explicitly select which one you want. You could make these into aliases or shell scripts if you wanted.
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.pyThe following is what I have been using successfully:
python2 /usr/local/bin/pylint p2file.py
python3 /usr/local/bin/pylint p3file.py 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 pylinthere you can then figure out where your env has put your python and pylint by
which pylint #/home/$USER/Desktop/python/awesomeSauce/bin/pylintand then
which python #/home/$USER/Desktop/python/awesomeSauce/bin/pythonThen 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.
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.
- Option 1: See "Option 1 Installation steps" below.
To get the latest version of
pylintfor Python3, you must manually download the latest release from GitHub, extract it, and then run this to install it:
The version will be# Install it sudo python3 setup.py install # Check its version pylint --versionpylint 2.12.2or later and the executable for Python3 will be calledpylint. There is no executable for Python2, since Python2 is obsolete and has been "sunset", or deprecated, since 1 Jan. 2020.
This version ofpylintshows code numbers for problems it finds in your code, such asC0301orW0105, as shown below. - Option 2: See "Option 2 Installation steps" below.
- To get an older version of
pylint, such aspylint 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 - To get an older version of
pylint3, such aspylint3 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 - 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
pylintfor Python2 code andpylint3for Python3 code.
- To get an older version of
Details
Resources:
- GitHub source code:
- Pylint User Manual:
- Tutorial: . Notice that the
pylintoutput 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.
- Tutorial: . Notice that the
[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:
- Go to the "Releases" page for
pylinton GitHub, and download the source code for the latest release (ex: v2.12.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. cdinto the extracted directory and run thesetup.pyscript; ex:
See also:cd path/to/pylint-2.12.2 sudo python3 setup.py install- Check your version and ensure it now says
pylint 2.12.2pylint --version - 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 pylint3Now 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.pyThe 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/pylinteRCaGuy_hello_world/python$ which pylint3
/usr/bin/pylint3eRCaGuy_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')() )