[Python] build Python development environment in vscode

Time:2020-10-20

preface

I used it beforeAnacondaUnder theSpyderPycharmandJupyterWait for me to writepythonEach project has its own advantages and disadvantages. Because of mineC++andGolangProjects are usedVSCodeCLionGolangandPycharmThey all need to be paid to use, andMacResource consumption ratioVSCodeSo it’s just going to be biggerMacandWindowsOfpythonProjects are migrated toVSCodeGo ahead.

install

First, make sure the computer is installedPythonandVSCode

1. Install the python plug-in of vscode

[Python] build Python development environment in vscode

2. Select the Python interpreter

ctrl+shift+PopenVSCodeCommand line, enterpython: select InterpreterChoose the right onepythonedition.
[Python] build Python development environment in vscode

You can see in the.vscode/launch.jsonMedium,pythonThe installation directory of has been added, which is different from the user’s global settings

{
    "python.pythonPath": "C:\Users\YANG\AppData\Local\Programs\Python\Python37\python.exe"
}

3. Running code

Now I’ve written a small project and used itgitHosting, inmainFirst of all, ensure that the file organization format is correct
[Python] build Python development environment in vscode

staywindowsYou can run the project directly from the command line

cd ./src
python test.py

[Python] build Python development environment in vscode

stayvscodeTo run the project code, you just need to right-click the run code, which is still in the call environment variablepython.exeExecute the correspondingpythonDocuments.

Configure Python check items

pythonCode error checking forpep8pylintandflake8Automatic formatting code is usually usedautopep8yapfblack

  • Install the corresponding library
pip install flake8
pip install yapf
  • Modify vscode’s configuration file
{
    "python.pythonPath": "/usr/bin/python",
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.pylintEnabled": false,
    "python.formatting.provider": "yapf"
    " python.linting.flake8Args ": [" -- Max line length = 248 "], set the maximum length of each line
}

If you keep reporting errors:Linter pylint is not installed, then invscodeBottom leftpython Choose an appropriate interpreter from the interpreter and wait for it to be installed automatically.
[Python] build Python development environment in vscode