VS Code搭建Python開發(fā)環(huán)境

前言

日常工作中,我用的比較多的Python IDE就是Pycharm和VS Code了。Pycharm雖然內(nèi)存占用大,但是不得不說真心好用。一些小的項(xiàng)目我就會換上VS Code,輕量級嘛,不過要在VS Code的中調(diào)試Python,需要安裝Python插件。

Python插件

VS Code1.3版中對插件的安裝做了調(diào)整,新增了安裝面板,感覺比之前好用多了。直接搜索Python插件安裝即可,它提供智能補(bǔ)全、函數(shù)跳轉(zhuǎn)、DEBUG、Lint、自動(dòng)格式化等常用功能。

VS Code.png

配置

插件安裝完后,我們可以在默認(rèn)配置中發(fā)現(xiàn)下面一堆Python的配置。

// Python Configuration

    // Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
    "python.pythonPath": "python",

    // Whether to lint Python files.
    "python.linting.enabled": true,

    // Whether to lint Python files using prospector.
    "python.linting.prospectorEnabled": false,

    // Whether to lint Python files using pylint.
    "python.linting.pylintEnabled": true,

    // Whether to lint Python files using pep8
    "python.linting.pep8Enabled": false,

    // Whether to lint Python files using flake8
    "python.linting.flake8Enabled": false,

    // Whether to lint Python files using pydocstyle
    "python.linting.pydocstyleEnabled": false,

    // Whether to lint Python files when modified.
    "python.linting.lintOnTextChange": true,

    // Whether to lint Python files when saved.
    "python.linting.lintOnSave": true,

    // Controls the maximum number of problems produced by the server.
    "python.linting.maxNumberOfProblems": 100,

    // Severity of Pylint message type 'Convention/C'.
    "python.linting.pylintCategorySeverity.convention": "Hint",

    // Severity of Pylint message type 'Refactor/R'.
    "python.linting.pylintCategorySeverity.refactor": "Hint",

    // Severity of Pylint message type 'Warning/W'.
    "python.linting.pylintCategorySeverity.warning": "Warning",

    // Severity of Pylint message type 'Error/E'.
    "python.linting.pylintCategorySeverity.error": "Error",

    // Severity of Pylint message type 'Fatal/F'.
    "python.linting.pylintCategorySeverity.fatal": "Error",

    // Path to Prospector, you can use a custom version of prospector by modifying this setting to include the full path.
    "python.linting.prospectorPath": "prospector",

    // Path to Pylint, you can use a custom version of pylint by modifying this setting to include the full path.
    "python.linting.pylintPath": "pylint",

    // Path to pep8, you can use a custom version of pep8 by modifying this setting to include the full path.
    "python.linting.pep8Path": "pep8",

    // Path to flake8, you can use a custom version of flake8 by modifying this setting to include the full path.
    "python.linting.flake8Path": "flake8",

    // Path to pydocstyle, you can use a custom version of pydocstyle by modifying this setting to include the full path.
    "python.linting.pydocStylePath": "pydocstyle",

    // Arguments passed in. Each argument is a separate item in the array.
    "python.linting.prospectorArgs": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.linting.pylintArgs": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.linting.pep8Args": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.linting.flake8Args": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.linting.pydocStyleArgs": [],

    // The output window name for the linting messages, defaults to Python output window.
    "python.linting.outputWindow": "Python",

    // Provider for formatting. Possible options include 'autopep8' and 'yapf'.
    "python.formatting.provider": "autopep8",

    // Path to autopep8, you can use a custom version of autopep8 by modifying this setting to include the full path.
    "python.formatting.autopep8Path": "autopep8",

    // Path to yapf, you can use a custom version of yapf by modifying this setting to include the full path.
    "python.formatting.yapfPath": "yapf",

    // Arguments passed in. Each argument is a separate item in the array.
    "python.formatting.autopep8Args": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.formatting.yapfArgs": [],

    // Format the document upon saving.
    "python.formatting.formatOnSave": false,

    // The output window name for the formatting messages, defaults to Python output window.
    "python.formatting.outputWindow": "Python",

    // List of paths to libraries and the like that need to be imported by auto complete engine. E.g. when using Google App SDK, the paths are not in system path, hence need to be added into this list.
    "python.autoComplete.extraPaths": [],

    // Whether to enable or disable unit testing using nosetests.
    "python.unitTest.nosetestsEnabled": false,

    // Path to nosetests, you can use a custom version of nosetests by modifying this setting to include the full path.
    "python.unitTest.nosetestPath": "nosetests",

    // Whether to enable or disable unit testing using pytest.
    "python.unitTest.pyTestEnabled": false,

    // Path to pytest, you can use a custom version of pytest by modifying this setting to include the full path.
    "python.unitTest.pyTestPath": "pytest",

    // Arguments passed in. Each argument is a separate item in the array.
    "python.unitTest.nosetestArgs": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.unitTest.pyTestArgs": [],

    // Arguments passed in. Each argument is a separate item in the array.
    "python.unitTest.unittestArgs": [],

    // Whether to enable or disable unit testing using standard unittest (built into Python).
    "python.unitTest.unittestEnabled": true,

    // The output window name for the unit test messages, defaults to Python output window.
    "python.unitTest.outputWindow": "Python",

我一般會在用戶配置或者項(xiàng)目配置中設(shè)置下面幾項(xiàng):

"python.pythonPath": "python",

"python.linting.enabled": true,

"python.linting.pylintEnabled": false,

// lint使用pep8
"python.linting.pep8Enabled": true,

// 如果使用virtualenv的話,可能要指定具體路徑
"python.linting.pep8Path": "pep8",

"python.linting.lintOnSave": true,

"python.formatting.provider": "yapf",

// 同樣,使用virtualenv的話,要指定具體路徑
"python.formatting.yapfPath": "yapf"

記得pip安裝pep8和yapf,豆瓣源https://pypi.douban.com/simple

DEBUG

Python插件的debug和nodejs是類似的,都不要一個(gè)launch.json的配置,分為Python、Python Console App 、Django和Watson。
我一般就用Python的。

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath": "python",
    "program": "${workspaceRoot}/runserver.py",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ] 
}

如果使用的是virtualenv或者其他不適默認(rèn)Python路徑的,需要指定pythonPath。

然后就可以愉快的開始DEBUG了。

結(jié)語

其實(shí)不管是什么IDE都只是工具而已,可能有些用的熟了是能提高工作效率,但是提高的工作效率是否能彌補(bǔ)真正的能力,我也不知道了。。。

最后,如果想過濾掉.pyc文件和.git目錄或者其他一些文件或者目錄的,請使用files.exclude配置。

"files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.DS_Store": true,
    "**/*.pyc": true
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容