SoFunction
Updated on 2025-05-16

An article explains how to run Python using VScode

Preface

In the world of programming, choosing the right tool can significantly improve development efficiency. For Python developers, Visual Studio Code (VSCode for short) has become the editor of choice for many people due to its powerful features and rich plug-in support. This article will introduce in detail how to write and run Python code in VSCode to help you get started quickly and improve your programming experience.

Why choose VSCode?

Before we start, let's take a look at why more and more Python developers choose VSCode:

  • Cross-platform: VSCode supports Windows, macOS and Linux, and can switch seamlessly no matter which operating system you use.
  • Lightweight and efficient: Compared with some heavyweight IDEs, VSCode starts faster and takes up less resources.
  • Rich plug-in ecosystem: VSCode has a huge plug-in market, and can expand functions by installing various plug-ins to meet different development needs.
  • Powerful debugging tools: Built-in debugging features can help you find and fix errors in your code more efficiently.
  • Code Intelligent Sensation: Supports functions such as code completion, syntax highlighting, code navigation, etc. to improve coding efficiency.

Install VSCode

First, you need to download and install VSCode. accessVSCode official website, select the corresponding version according to your operating system to download. The installation process is very simple, just follow the prompts to complete.

Install Python plugin

In order to better write and run Python code in VSCode, we need to install Python plugin. Here are the installation steps:

  • Open VSCode.
  • Click the extension icon (the icon composed of four squares) in the activity bar on the left.
  • Enter "Python" in the search box.
  • Find the Python plugin provided by Microsoft and click "Install".

After the installation is complete, VSCode will automatically restart to apply the new plug-in.

Configure Python environment

Before you start writing your code, make sure that you have Python installed on your computer. You can accessPython official websiteDownload and install the latest version of Python.

Setting up Python interpreter

  • Open VSCode.
  • according toCtrl+Shift+P(Windows/Linux) orCmd+Shift+P(macOS) Open the command panel.
  • Enter "Python: Select Interpreter" and press Enter.
  • Select one from the listed interpreters. If you have multiple Python versions, you can choose the version you wish to use.

Create Python files

  • In VSCode, click the Explorer icon (folder icon) on the left.
  • Right-click on the workspace and select "New File".
  • Enter a file name, for example, and press Enter.

Writing Python code

existEnter the following code into the file:

print("Hello, World!")

Run Python code

Run with terminal

  • Open VSCode's integrated terminal (click "Terminal" -> "New Terminal" in the top menu bar).

  • Enter the following command in the terminal and press Enter:

    python 
    

    If everything works fine, you should see the output in the terminal:

    Hello, World!
    

Run Python File Features with VSCode

  • existIn the file, right-click the code area.
  • Select "Run Python File in Terminal".
  • VSCode will automatically run your Python code in the terminal and display the output results.

Debugging Python code

VSCode provides powerful debugging functions that can help you debug your code more efficiently. Here are the basic debugging steps:

  • Set breakpoints in the code. Click on the blank area to the left of the code line number and a red dot will appear, indicating that the breakpoint has been set.
  • Open the debug view. Click the bug icon in the activity bar on the left.
  • Click the "Create a file" link and select "Python File".
  • VSCode will generate aFile containing debug configuration. You can modify the configuration as needed.
  • Click the green triangle button in the debug view to start debugging.

During the debugging process, you can view variable values, step through code, skip or continue execution, etc.

Advanced skills

Code formatting

Keeping your code tidy is the key to improving code readability and maintenance. VSCode supports a variety of code formatting tools, such as Black, Autopep8, etc. After installing these plugins, you can format the code by right-clicking on the code area and selecting "Format Document".

Code snippet

Code snippets can help you quickly insert commonly used code snippets and improve coding efficiency. You can customize code snippets in VSCode, or install ready-made code snippet plugins.

Git integration

VSCode integrates Git version control function, you can directly perform version control operations in VSCode, such as submission, pull, push, etc. This greatly simplifies the process of code management and collaboration.

Unit Testing

Unit testing is an important means to ensure the quality of the code. VSCode supports a variety of Python testing frameworks, such as unittest, pytest, etc. After installing the corresponding plug-in, you can run and debug test cases directly in VSCode.

Practical cases

In order to better understand how to write and run Python code in VSCode, let's take a look at a practical case. Suppose we want to write a simple web crawler, grab the title of a certain website and print it out.

Installation dependencies

First, we need to installrequestsandbeautifulsoup4Library. Enter the following command in the terminal:

pip install requests beautifulsoup4

Writing code

Create a new Python fileweb_scraper.py, and enter the following code in it:

import requests
from bs4 import BeautifulSoup

def get_title(url):
    response = (url)
    if response.status_code == 200:
        soup = BeautifulSoup(, '')
        title = 
        return title
    else:
        return None

url = ''
title = get_title(url)
if title:
    print(f'Title: {title}')
else:
    print('Failed to retrieve the title.')

Run the code

  • Enter the following command in the terminal and press Enter:

    python web_scraper.py
    
  • If everything works fine, you should see the output in the terminal:

    Title: Example Domain
    

Through the introduction of this article, I believe you have mastered the basic methods of writing and running Python code in VSCode. Whether it is a beginner or an experienced developer, VSCode can provide you with powerful support to help you complete programming tasks more efficiently.

Of course, learning is endless. If you want to further improve your Python programming skills, you might as well consider taking some professional training courses. For example, CDA data analysis certification training provides a wealth of Python programming courses, covering multiple fields such as data processing, data analysis, machine learning, etc., which is very suitable for friends who want to develop in the field of data science.

Summarize

This is the end of this article about how to write Python with VScode. For more related content on writing Python with VScode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!