Visual Studio Code is an open-source, customizable, lightweight editor available across all platforms and one of the best Python Online Compilers accessible to programmers. Because of these features, Microsoft’s Visual Studio Code is incredibly well-liked and a great environment for Python development.
This post will show you how to build, run, and debug Python script code files using Visual Studio Code.
- To Install The Python Extension For Visual Studio Code.
- Click on View and select the Extensions menu option from the menu bar of Visual Studio Code. The EXTENSIONS panel on the left side of Visual Studio Code will display a list of all installed and suggested extensions.
- To install the Python extension made by Microsoft, type “python” into the search bar and press the Install button.
- You must restart Visual Studio after you install the Python extension Code for it to take effect.
- However, you must ensure the Python interpreter has been appropriately installed on your OS before you begin writing and debugging Python code in Visual Studio Code.
- In Visual Studio Code, choose the installed Python interpreter.
- Click on the View and press the Command Palette button after starting Visual Studio Code.
- Type in the word “Python” and click Interpreter on the popup window that appears after you make your selection.
- It will then show every Python interpreter installed on your OS (the path to the python .exe file). One of these can be chosen to execute the Python script file afterward.
- Python Script Code File Development with Visual Studio Code.
- Make a folder on your operating system that will serve as the Visual Studio Code workspace for the Python project.
- Select Open Folder under File. In the folder, browse dialogue, select the newly created folder, and press the Open button. On the left side pane of Visual Studio source Code, which has the same name as the folder name in capital letters, all the files inside the folder will then be listed.
- To create a new python file, either right-click the folder name and choose New File from the popup menu list or click the New File button in the upper right corner of the folder pane. Ensure the File has a .py extension at the end.
- Tap the filename. The File will open in a new window on the right.
- You can now enter the Python code shown below in the newly built Python file.
msg = ‘I love python’
- How You Can Use Visual Studio Code To Execute The Python Script File.
- A menu list will appear when you right-click the source code of the Python source file in the editor.
- A few menu options allow you to run and execute the Python code in various settings.
- The Run and execute the Python Script in Terminal menu option launches the Terminal built into Visual Studio Code and executes the python code.
- When you select this menu item, the TERMINAL screen at the base of the visual studio code will appear. Then open the Terminal and execute the python file.
- The TERMINAL window displays the result text you can see below.
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 https://support.apple.com/kb/HT208050.
(base) songs-MacBook-Pro:PythonExample songzhao$ /usr/local/bin/python3 /Users/songzhao/Documents/WorkSpace/PythonExample/test.py
I love python
- Select Terminal > New Terminal / Split Terminal to open a new TERMINAL window on the top menu bar.
- An interactive window will open on the right-hand side of the Visual Studio code once you right-click the Python source file and choose Run Existing File in the Interactive Window from the popup menu list.
- A text line is present. To run the code in a dark blue region at the bottom of the interactive window, type it here and press shift-enter.
- To execute the Python code, insert the source code and click Shift + Enter.
- You can view the Python code running the result in the active window top area.
- When you simultaneously press the Shift and enter keys, the interactive window’s top area will show both the result and the Python source code.
- Python Source Code Debugging with Visual Studio Code.
- To establish a breakpoint, click the first column of a Python source code line.
- To begin debugging, click on Visual Studio Code Run and select the Start Debugging menu item in the menu bar.
- The command palette window will now be opened. Choose Python File Debug the currently active Python file from the drop-down list.
- The first breakpoint will cause the Python program to terminate.
- To debug the Python code, click on Continue, Step Into, Step Over, Step Out, Restart, and Stop buttons on the debug toolbar.