Installing Pylint for Python3 on Ubuntu
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?
Solution 1:
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')()
)
Solution 2:
@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.
Solution 3:
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
Solution 4:
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.
Solution 5:
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