The article directories

  • The foreword 0.
  • 1. Install Python
  • 2. Install Python extensions
  • 3. Create a workspace
  • 4. Edit code
  • 5. Run the Python program
    • 5.1. Configure the Python interpreter
    • 5.2. Run the entire Python file
    • 5.3. Run some Python code
  • 6. Debug Python programs
    • 6.1. Set up the debugger
    • 6.2. Set breakpoints
    • 6.3. Start debugging
  • 7. Use the Python interactive environment
  • 8. Install and use packages

The foreword 0.

  • VS Code is a lightweight tool for easy editing, compiling, and running Python programs, especially when learning Python.
  • If you need to develop Python projects, it is recommended to use the specialized Python IDE — Pycharm.

1. Install Python

2. Install Python extensions

VS Code’s Python extension has the following features:

  • Intelligent perception;
  • Code debugging;
  • Code navigation;
  • Support for Jupyter Notebook;

Expand market search in VScodepython, click Install:



3. Create a workspace

VS Code uses folders as workspaces, so there are two ways to create workspaces:

  • On the Windows command line, go to the folder and use the commandcode .Open the folder and create a workspace;
  • First open VS Code, then use VS Code to open the folder and create the workspace;

So let me create one herepy_projectFolder and open in VS Code as workspace:

4. Edit code

Add the file helloworld.py to your workspace and edit the following code:

def hello() :
	print("HelloWorld")

if __name__ == '__main__':
	hello()
Copy the code

When editing code, the Python extension’s code awareness looks like this:

5. Run the Python program

5.1. Configure the Python interpreter

When you open the Python file, VS Code automatically selects the Python interpreter configured on the system and displays it in the status bar, as shown in the following figure:



If you need to switch interpreters, useCtrl+Shift+POpen the command panel and enterpython, the choice ofPython:Select Interpreter:



5.2. Run the entire Python file

  • inCode editorMedium: Right-click any location and choose from the shortcut menuRun the Python file on the terminal;



  • inResource managerIn: Right-click the Python file and chooseRun Python on the terminal:

5.3. Run some Python code

First select the code you want to run, then right-click and selectRun the selected content/line in the Python terminal:

The running results are as follows:

6. Debug Python programs

Python extensions support debugging:

  • Set breakpoints, examine data, and use the debug console as you step through the program;
  • Debug many different types of Python applications, including: multithreaded, Web, and remote applications;

6.1. Set up the debugger

First switch to the debug screen and click the debugger Settings button:



Then select the debugger:



When you select the debugger VS Code is automatically displayed in the workspace.vscodeDirectory creationlaunch.jsonFile:



If you want to start debugging automatically stop in the program entry, add the following Settings:

"stopOnEntry": true
Copy the code

6.2. Set breakpoints

6.3. Start debugging

After debugging starts, the program stops at the first breakpoint:

Debug using the Debug Control panel:



The buttons in the control panel are, from left to right:

  • Move button: after dragging, you can move the debugging control panel;
  • Continue running (F5);
  • Step skipping (F10);
  • Single step debugging (F11);
  • Step out (Shift+F11);
  • Restart (Ctrl+Shift+F5);
  • Stop debugging (Shift+F5);

VS Code is on the leftVariable view window, monitor view window, stack view window:

7. Use the Python interactive environment

useCtrl+Shift+POpen the command panel and enterpython, the choice ofPython:Start REPL:



The interactive environment REPL launched is as follows:

8. Install and use packages

Using packages in Python (Python Package index-pypi) to extend very rich functionality, an example of using packages and installing packages in VS Code is shown below ~

Start by creating a new file standardplot.py and edit the following code:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.20.100)   Create a list
plt.plot(x,np.sin(x))       # Plot the sine of each point
plt.show()                  # show
Copy the code

Then run the code:

Because we use the Matplotlib and Numpy libraries in our code, but we don’t have them installed on our system, the interpreter tells us we can’t find the module, so install them.

First press ‘Ctrl+Shift+’ to create a new integration terminal (command line), then use the following command to install the package:

# macOS
python3 -m pip install matplotlib

# Windows 
python -m pip install matplotlib

# Linux (Debian)
apt-get install python3-tk
python3 -m pip install matplotlib
Copy the code



It can be used after installationpip listView the installed packages in the current system and confirm that the installation is successful:

Run the Python program again, and the result looks like this: