Visual Studio Code (VS Code) is a powerful and lightweight code editor developed by Microsoft, supporting multiple programming languages, including Python. In this guide, we will walk through the step-by-step process of setting up VS Code for Python development, writing Python scripts, and debugging them effectively.
1. Installing and Configuring VS Code for Python.
Before writing Python code, you need to install VS Code and configure it for Python development.
1.1 Open VS Code.
- After installing VS Code, you can launch it by double-clicking the desktop icon or searching for “VS Code” in the Windows Start menu.
1.2 Install Python Extension.
- To enable Python support in VS Code, follow these steps:
- Click on the Extensions icon on the left sidebar (or press `Ctrl+Shift+X`).
- Type Python in the search bar and hit Enter.
- Locate the extension provided by Microsoft and click Install.
- Once installed, this extension will provide features like syntax highlighting, debugging, and code completion.
2. Selecting the Python Interpreter.
After installing the Python extension, you need to select the correct Python interpreter.
2.1 Open Command Palette.
- Click on View -> Command Palette or press `Ctrl+Shift+P`.
2.2 Choose Python Interpreter.
- In the command palette, type Python: Select Interpreter and click on the suggested option.
- A list of available Python versions will appear.
- Select the appropriate Python version (e.g., Python 3.12.8).
3. Creating and Running a Python File.
Now that the setup is complete, let’s create and run a Python script.
3.1 Create a New Python File.
- Click on File -> New File.
- Choose Python File as the file type.
- Write the following Python code:
print("Hello, World!")
- Save the file using `Ctrl+S`, name it test.py, and select a save location.
3.2 Run the Python File.
- Right-click anywhere in the editor and select Run Python File in Terminal.
- The output should be displayed in the terminal:
Hello, World!
4. Debugging Python Code in VS Code.
VS Code provides debugging features that help identify issues in your Python scripts.
4.1 Setting a Breakpoint.
- Click on the left margin next to the line number to set a breakpoint.
- A red dot will appear, indicating that execution will pause at this point.
4.2 Start Debugging.
- Click the Run —> Start Debugging button on the left sidebar.
- Choose Python File as the debugging configuration.
- The code will execute and pause at the breakpoint, allowing you to inspect variables and step through the execution.
5. Conclusion.
In this guide, we covered how to set up VS Code for Python development, select the Python interpreter, create and run Python scripts, and use debugging tools. With these steps, you can efficiently write and troubleshoot Python code using VS Code.
6. Demo Video.
You can watch the following demo video by select the subtitle to your preferred subtitle language.