30

I'm using Sublime Text 3 With Pylinter to run pylint on my files.

However, on the same machine, I work on files for both python 2, and python 3 projects (the code is executed on one of several remote test-VMs, via SSH. I'm modifying the files by opening them over SMB. This is my home test-lab, and I'm finally sitting down to learn py3k).

Can I easily override the mechanism pylint uses to determine the python version it should lint for? Ideally, there would be an inline directive, but I have not had much luck finding anything.

I'm (editing) on Windows (the remote VMs are linux, but that' irrelevant here), for what it's worth.

gurney alex
  • 13,247
  • 4
  • 43
  • 57
Fake Name
  • 5,556
  • 5
  • 44
  • 66

7 Answers7

34

You can try python2 -m pylint ... and python3 -m pylint .... That ensures that you use the right version.

chris
  • 1,831
  • 18
  • 33
  • 3
    Perfect, I think this is the best answer. Of course you have to install pylint on both of your python installation. (`pip2 install pylint` and `pip3 install pylint`) – John Smith Optional Mar 23 '18 at 13:15
  • This method will add the current directory to the path, so make sure it doesn't contain a file called `pylint.py` or conflict with any indirect imports like `astroid` (probably a safe assumption for your own code, but dangerous if trying to automatically analyse third party repos) – anjsimmo Jul 05 '20 at 10:38
24

AFAIK Pylint lints for the version of Python it is running on and it is not possible to override it.

gurney alex
  • 13,247
  • 4
  • 43
  • 57
  • @gurney-alex is right. This is because pylint rely both on the parser and the ast representation provided by the python distribution. – sthenault Apr 15 '14 at 06:41
  • 1
    I see. I didn't realize how deeply it hooked into the python parser to work. I assumed it was interpreting the code internally, rather then operating on the AST generated by running the local python interpreter against it. – Fake Name Apr 15 '14 at 06:48
  • I'm not sure this is entirely correct. I am running Python 2.7.3, yet pylint reports `warning (W0622, redefined-builtin, ) Redefining built-in 'bytes'`. `bytes()` is not a built-in in python 2. – nullromo Aug 10 '16 at 23:54
6

Expanding on @sthenault's answer and borrowing heavily from @simon's to a very similar question on askubuntu, the solution is to write a wrapper script around pylint that executes it with the appropriate version of the Python interpreter. Drop the following into a script called mypylint (or whatever) somewhere in your $PATH:

#! /usr/bin/env bash

python_interpreter="python${1}"
pylint_args="-f colorized ${@:2}"
pylint_import=$(cat << PYTHON
import sys
import pkg_resources

__requires__ = "pylint"
sys.exit(
    pkg_resources.load_entry_point("pylint", "console_scripts", "pylint")()
)
PYTHON
)

$python_interpreter -c "$pylint_import" $pylint_args

Then, execute it like so: mypylint 2|3 PYLINT_ARGS. For instance:

mypylint 2 -f colorized module.py

I'm not sure how you can tie that into sublime-text, but it more generally answers the question of parallel versions of pylint. I also bundled the above solution into a gist.

Community
  • 1
  • 1
sevko
  • 1,402
  • 1
  • 18
  • 37
3

This is good, but I think the simplest thing is just to use virtualenv, and install pylint in each virtualenv. The correct pylint and python interpreter will be used.

user3685621
  • 641
  • 6
  • 4
  • Thanks. I had virtualenv and used pylint but it wasn't installed in my virtualenv so pylint used the global one instead which caused problems. – E. Sundin Nov 18 '16 at 12:39
1

You can override on a per-project level in Sublime Text by changing the pylint executable setting in Project->Edit Project to include:

"settings":
{
    "SublimeLinter.linters.pylint.executable": ["py", "-3.4", "-m", "pylint"],
}

substituting 3.4 for your preferred flavour

  • on my windows machine the command is instead `["C:\\python27\\python.exe", "-m", "pylint"]`, which uses the literal Python executable path -> can use the literal path for your particular python.exe of interest – CrepeGoat Apr 22 '20 at 05:08
  • ... for example, that of a virtualenv – CrepeGoat Apr 22 '20 at 05:18
0

You should have two pylint installations, say pylint2 and pylint3, then write a wrapper script that will subprocess the desired one.

sthenault
  • 14,397
  • 5
  • 38
  • 32
0

You can install pylint3 which will evaluate for python 3.0, and pylint which will evaluate the code as python 2.7 by default.

David Greydanus
  • 2,551
  • 1
  • 23
  • 42