1. 程式人生 > >解決sublime使用pylint外掛時對django的支援問題

解決sublime使用pylint外掛時對django的支援問題

Sublime是我最愛的寫程式碼神器。最近看網上配置sublime為python IDE的文章時,看到很多人推薦使用Pylinter這個外掛。仔細研究了一番,這個外掛主要是用來檢查程式碼格式的,從而規範化python程式設計師的編碼規範。

但是由於我經常開發的專案是Django,而Django各種ORM把pylint搞得是暈頭轉向。比如,經常會出現這樣一個錯誤:


Class 'xxx' has no 'objects' member

這個其實就是pylint對Django支援不好的原因。看StackOverFlow大神說需要使用一個“更懂Django”的外掛,叫做pylint-django

Ubuntu使用如下命令安裝:

sudo pip install pylint-django

然後使用的時候這麼使用,在命令列

pylint -E --load-plugins=pylint_django xxx.py

-E是隻輸出錯誤資訊的意思


可以看到還是有效果的。加了pylint_django的外掛以後就不輸出錯誤。但是當在sublime pylint中配置load_plugins時,就成了這樣:


出現pylint_plugin_utils.NoSuchChecker錯誤。。

好吧,根據逐一排查,最終定位到原來問題的根源在於/usr/local/lib/python2.7/dist-packages/pylint_plugin_utils/__init__.py檔案中的一個if判斷句

臨時解決辦法:

把__init__.py檔案中的get_checker函式中的

if isinstance(checker, checker_class):

替換為

if checker.__class__.__name__ == checker_class.__name__:


問題解決。。。

附上我的Sublime的Pylinter外掛的User.settings

{
    // When versbose is 'true', various messages will be written to the console.
    // values: true or false
    "verbose": false,
    // The full path to the Python executable you want to
    // run Pylint with or simply use 'python'.
    "python_bin": "python",
    // The following paths will be added Pylint's Python path
    "python_path": [
        "/usr/bin/python",
        ],
    // Optionally set the working directory
    "working_dir": null,
    // Full path to the lint.py module in the pylint package
    "pylint_path": "/usr/local/lib/python2.7/dist-packages/pylint/lint.py",
    // Optional full path to a Pylint configuration file
    "pylint_rc": "/home/myusername/myproject/pylint.rc",
    // Set to true to automtically run Pylint on save
    "run_on_save": true,
    // Set to true to use graphical error icons
    "use_icons": true,
    "disable_outline": false,
    // Status messages stay as long as cursor is on an error line
    "message_stay": false,
    // Ignore Pylint error types. Possible values:
    // "R" : Refactor for a "good practice" metric violation
    // "C" : Convention for coding standard violation
    // "W" : Warning for stylistic problems, or minor programming issues
    // "E" : Error for important programming issues (i.e. most probably bug)
    // "F" : Fatal for errors which prevented further processing
    "ignore": ["C", "R",],
    // a list of strings of individual errors to disable, ex: ["C0301"]
    "disable": ["W0702",],
    "plugins": ["pylint_django",],
}