[Python] Build a Python development environment in VSCode

Posted May 26, 20202 min read

Foreword

I have used Spyder, Pycharm and Jupyter under Anaconda before to write python data analysis projects, each has advantages and disadvantages. Because my C ++ and Golang projects both use VSCode(CLion, Golang and Pycharm all need to be paid to use, and consume more resources on Mac than VSCode) , So simply migrate the python projects of Mac and Windows to VSCode.

installation

First make sure that the computer has Python and VSCode installed

1 . Install Python plugin for VSCode

2. Select the Python interpreter

ctrl + shift + P Open the command line of VSCode and enter python:select Interpreter to select the appropriate python version.

It can be seen that in .vscode/launch.json, the installation directory of python 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 . Run the code

Now I have written a small project and use git hosting, under the main function, first make sure that the file organization format is correct:

Under windows you can directly run the project using the command line:

cd ./src
python test.py

In vscode, you only need to right-click to run the code to run the project code. Essentially it is still calling python.exe in the environment variable to execute the corresponding python file.

Configure python check items

The code error checking of python usually uses pep8, pylint and flake8, and the auto formatting code usually uses autopep8, yapf, black.

  • Install the corresponding library

    pip install flake8
    pip install yapf

  • Modify the configuration file of vscode

    {

      "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 getting an error:Linter pylint is not installed, then select a suitable interpreter in the python interpreter in the lower left corner of vscode and wait for it to be installed automatically.