VSCode & PyCharm

Table of Contents

1. VSCode

(1) Commonly used plug-ins (extensions)

Django, Django Template, Pylance, Python, Code Runner

(2) Issues related to plug-ins (extensions)

1. ErrorThe Pylance server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.

reason:

solve:

2. Python code highlighting exception (dependencies cannot be used, text is displayed in white)

The reasons and solutions are the same as above

(2) Shortcut keys

(3) Before executing debug

(4) launch.json & settings.json (.vscode folder)

1. Folder.vscode

2. launch.json

3. settings.json

(5) Use and creation of virtual environment

2. PyCharm

(1) Set Encoding

(2) Configuring virtual machines

1. Create a virtual machine

2. View all virtual machines

3. Configure the virtual machine environment

(3) Terminal

1. Enter the terminal

2. Operate Command Terminal

(1) Generally, the local terminal is used by default

(2) If the file path starts with PS, it is a shell terminal

(3) Switch to Command terminal

(4) Shortcut keys

1. Standardize code format


VSCode: suitable for front-end and back-end development.

PyCharm: suitable for backend development of python projects.

The following contents are all explained for the development of Python-Django project.

1. VSCode

(1) Commonly used plug-ins (extensions)

Django, Django Template, Pylance, Python, Code Runner

Notice:

Different versions of python environments require different plug-in versions. If the python version changes, you need to reselect the plug-in version, especially for the python plug-in and Pylance plug-in.

for example:

In python3.11 environment, applicable python plug-in: the latest version is sufficient.

If the environment is changed to python3.6.8, reselect the applicable python plug-in (switch to another version, i.e. v2022.8.1). Otherwise, the normal debugging operation will not be possible because too high a version of the plug-in is used.

Similarly: the Pylance plug-in version applicable to python3.6.8 is v2023.11.10.

1. ErrorThe Pylance server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.

Reason:

The python environment does not support the Pylance plug-in.

Resolution:

(1) Re-select and install the Pylance plug-in version corresponding to python.

(2) settings.json configuration (in the .vscode folder).

{
 // ...
"python.languageServer": "Pylance",
}

2. python code highlighting exception (dependency cannot be used, text is displayed in white )

The reasons and solutions are the same as above

(2) Shortcut keys

1. Jump definition

F12 or Ctrl + left mouse click

2. Jump to mouse position

Alt + ←: Return to the previous mouse position

Alt + →: Next

3.debug shortcut keys

F5: Start debug (or find the debug function in the left navigation bar of vscode and start it with the small green triangle)

F10: Single step skip, execute each line of the current page.

F11: Single-step execution, execute every line inside each page including the jump in func.

(3) Before executing debug

To debug the project, you need to configure the launch.json file. This will be explained in launch.json in Section (4) of this article.

If you modify the port port for ip, add it in the args attribute of the configurations in the launch.json document. As shown below:

Official reference document: Debugging in Visual Studio Code

(4) launch.json & settings.json (.vscode folder)

The two json configuration files are both stored in the .vscode folder in the root directory.

1. Folder.vscode

Location: placed in the root directory (same level as manage.py)

2. launch.json

(1) Function: Set running and debugging modes

(2) Establish:

① Select “Run” in the top navigation bar > Add configuration > Select python > Select Django. The .vscode folder and launch.json file will be automatically generated in the root directory.

② “Run & Debug” in the left navigation bar (bug icon) > Create launch.json file > Select python > Select Django.

(3) Basic content:

{
  // Use IntelliSense to learn about related properties.
  //Hover to see the description of an existing property.
  // For more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "LyDjango",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}\manage.py",
      "args": ["runserver", "0.0.0.0:8000"],
      "django": true,
      "justMyCode": true
    }
  ]
}
3. settings.json

(1) Function: Set code writing specifications, etc.

(2) Create: If there is no .vscode folder, create a new .vscode folder and settings.json file yourself

(3) Basic content:

{
  "git.autofetch": true,
  "flake8.args": [
    "--max-line-length=160",
    "--ignore=E722,E203,W291,W504,E131,E302"
  ],
  "black-formatter.args": [
    "--skip-string-normalization",
    "--line-length=120"
    // "--skip-magic-trailing-comma $FilePath$"
  ],
  "editor.formatOnSave": true,

  "[json]": {
    "editor.defaultFormatter": "ms-python.black-formatter"
  },
  "[jsonc]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter"
  },
  "jupyter.interactiveWindow.creationMode": "perFile",
  "python.analysis.typeCheckingMode": "off",
  "workbench.startupEditor": "none"
}

(5) Use and creation of virtual environment

Refer to another article: python installation, Django and other dependency installation, virtual environment configuration_is Luoluoganya’s blog-CSDN blog

二、PyCharm

(1) Set Encoding

The top navigation bar File > Settings > Editor > File Encodings > Project Encoding is set to UTF-8.

(2) Configuring the virtual machine

1. Create a virtual machine

Top navigation bar File>Settings>Select “Project: Select a python version) > Click OK and wait for creation > Reopen the terminal (you can switch to your own new virtual environment)

2. View all virtual machines

Cancel the filter in the red box below to see all virtual machines.

3. Configure the virtual machine environment

Top navigation bar Run > Edit Configurations > Set Path, parameter, python interpreter, working directory

(3) Terminal

1. Enter the terminal

In the bottom navigation bar, enter Terminal.

2. Operate Command Terminal

(1) Generally use the local terminal by default

Click the plus sign to generate a new terminal (i.e. Command terminal).

(2) If the file path starts with PS, it is a shell terminal

Need to switch to Command terminal (same as local terminal)

(3) Switch to Command Terminal

(4) Shortcut keys

1. Standardized code format

Ctrl + Alt + L

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treePreliminary knowledgeCommon development tools 385541 people are learning the system