How To Run Python Code In Visual Studio Code

This article will tell you the steps about how to use visual studio code to create, run and debug the python script files.

1. How To Install Visual Studio Code Python Extension.

  1. Open visual studio code, click View —> Extensions menu item at the top menu bar. It will list all the installed and recommend extensions in the visual studio code left side EXTENSIONS panel.
  2. Input the keyword python in the search box, and click the Install button for the Microsoft-created python extension to install it.
  3. You can also install the visual studio code python extension by clicking the Help —> Welcome menu item in the top menu bar, then click the Python link at the Customize —> Tools and languages area at the welcome page right side.
  4. After installing the visual studio code python extension, you need to restart the visual studio code to make it effective.
  5. But before you can start to make python coding in visual studio code, you need to make sure you have installed the python interpreter correctly on your OS.

2. Select Installed Python Interpreter In Visual Studio Code.

  1. You can select which python interpreter to use by clicking the select python environment option on the bottom left corner of the visual studio code status bar. If you had used one python environment, you can click it to change to use another python environment.
    visual studio code - select python envrionment option - on status bar
  2. When you click the above option, it will popup the Command Palette drop-down list, and display all the python interpreters installed on your OS. You can select one to run the python script later.
    visual studio code - command palette - show all python interpreters
  3. You can also implement the above process by clicking the visual studio code top menu bar View —> Command Palette… menu item, then input Python: Select Interpreter and click it in the drop-down list.

3. How To Create Python Script File In Visual Studio Code.

  1. Create a folder in your OS, this folder will be treated as the workspace for your visual studio code python project.
  2. Click File —> Open… or File —> Open Workspace… menu item in the visual studio code top menu bar. Browse the folder that you create and click the Open button in the folder browse dialog. Then it will list all the files in the folder on the visual studio code left side pane, the pane name is the uppercase of the folder name.
  3. Click the New File button on the top right corner of the folder pane to create a new python file. You should make sure the file ends with a .py extension.
  4. Click the file name, it will open the file on the right side.
  5. Now you can write python code in the created python file like below.
    msg = 'I love python'

4. How To Run The Python Script File In Visual Studio Code.

  1. Right-click the python file source code in the editor, it will popup a menu list.
  2. There are some menu items that can run the python file in different modes.
  3. The Run Python File in Terminal menu item will start the visual studio code built-in terminal and run the python source code in the terminal.
  4. When clicking this menu item, it will display the TERMINAL window at the bottom of the visual studio code. And run the python file in the terminal.
  5. You can see the output text like below in the TERMINAL window.
    /usr/local/bin/python3 /Users/songzhao/Documents/WorkSpace/PythonExample/
    The default interactive shell is now zsh.
    To update your account to use zsh, please run `chsh -s /bin/zsh`.
    For more details, please visit
    (base) songs-MacBook-Pro:PythonExample songzhao$ /usr/local/bin/python3 /Users/songzhao/Documents/WorkSpace/PythonExample/
    I love python
  6. If you want to create a new TERMINAL window, you can click the menu item Terminal —> New Terminal / Split Terminal on the top menu bar.
  7. If you click the Run Current File in Interactive Window menu item in the popup menu list when you right-clicking the python source file, it will open an interactive window on the right side of the visual studio code.
  8. There is a line of text Type code here and press shift-enter to run in a dark blue area at the interactive window bottom.
  9. You can input python source code in the dark blue area and then press shift + enter key to run the python code.
  10. You can see the python code executing result in the interactive window top area.
  11. For example, I input the below python source code in the interactive window input area.
    for i in range(10):
  12. When I press shift + enter keys at the same time, it will display both the python source code and result in the interactive window top area.

5. How To Debug Python Source Code In Visual Studio Code.

  1. Single-click the first column of the python source code line to set a breakpoint.
  2. Click the visual studio code Run —> Start Debugging menu item at the top menu bar to start the debugging process.
  3. Now it will open the command palette window, select the Python File Debug the current active Python file item in the drop-down list.
    visual studio code debug python file configurations
  4. The python program execution will stop at the first breakpoint.
  5. You can click the Continue, Step Over, Step Into, Step Out, Restart, Stop button in the debug toolbar to process the python code debugging.
    visual studio code debug toolbar
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
Fahad g

Excellent pal, Thanks for sharing such an informative Article, It was great reading this article. I would like to know more about 

Would love your thoughts, please comment.x