This article documents some VSCode Settings that are convenient for Python development and is constantly updated.

1. Set the indent to space

Click Command + Shift + P, enter setting, go to Preferences: Open Settings (JSON) and click Open Settings. Add this sentence to indent 4 Spaces:

"editor.tabSize": 4.Copy the code

2. Display the dots that represent Spaces

Open the Settings file with Command + Shift + P and add this sentence:

"editor.renderWhitespace": "all".Copy the code

Because Python relies heavily on indentation, it may sometimes not be obvious where a problem is if you don’t see the dots that represent Spaces.

3. Set shortcut keys for case conversion

These shortcuts come in handy when Python uses constants that correspond to a string and are represented in uppercase letters, which are the same as lowercase strings.

Set the shortcut for converting to lowercase: Control + Shift + L

Set the uppercase shortcut: Control + Shift + U

A:

Preferences -> Keyboard shortcuts

Click the button in the upper right corner to access the json file:

Enter the following:

[{"key": "ctrl+shift+u"."command": "editor.action.transformToUppercase"
    },
    {
        "key": "ctrl+shift+l"."command": "editor.action.transformToLowercase"}]Copy the code

Method 2:

To do this, type lowercase/ upperCase in the input box to search for a directive that matches lowercase/uppercase, click the button on the left of the instruction line to add or edit it, press the desired combination of shortcut keys, and then press Enter.

To set the shortcut for converting to lowercase, press Control + Shift + L and Enter:

To set the shortcut key for converting to uppercase letters, press Control + Shift + U and Enter:

In fact, from the keyboard shortcut panel you can see a lot of default shortcut commands, I tend to use the following ones:

Control + ‘brings up the terminal console

Command + Shift + N opens a new window.

Command + [moves the line to the left by an indentation

Command +] moves the line one indent to the right

Command + Shift + K deletes a line

4. Switch the Python interpreter

The interpreter used by the editor also needs to change when using different versions of Python. (You should already have the Python plug-in installed for Python development.)

First look at the file path of the current command:

$ which python3/ usr/local/opt/[email protected] / bin/python3Copy the code

A:

Open the Settings file with Command + Shift + P and add to the Settings file:

"python.pythonPath": "/ usr/local/opt/[email protected] / bin/python3." "
Copy the code

Method 2:

Click on the bottom left corner of the screen and enter the path you just found using which PYTHon3 in the input box that pops up.

Import “package_name” could not be resolved

When we use the virtual environment, the installation package is placed in the virtual environment path, and the project path is inconsistent, the following problems occur:

Import “package_name” could not be resolved and you cannot Command + Enter to the code in the package.

In this case, switch the Python interpreter to the Python directory in your project’s virtual environment as described above:

Hitting Command + Enter on Status takes you to the location defined by status in the REST_Framework package.

5. Use the code command to open the file on the command line

Click Command + Shift + P, type shell Command, find shell Command: Install ‘code’ Command in Path and click.

This allows you to open files on the terminal with code file_name.py, open folders with code dir, or use code under folders. Call up VSCode editor and open folder.

6. Copy the entire line

Use Option + Shift + up/down to copy rows up and down. If multiple rows are selected first, copy multiple rows above and below the row.

7. Multi-line editing

Select multiple lines and use Option + Shift + I to edit multiple lines simultaneously.

8. Select multiple lines

When there is a lot of content to select, if you select the code by long pressing the mouse, it will drag several screens, which is very inconvenient.

Multiple lines of code can be selected as if multiple files were selected in a folder:

Place the cursor at the beginning of the code you want to select, hold down the Shift key and click the mouse at the end of the code you want to select.