This procedure walks you through running a Python console program in Visual Studio Code.
🧰 Prerequisites
Set up all three before continuing. The steps below assume they are already in place.
- Visual Studio Code installed
- Python extension installed
- Python project created in VS Code
📋 Steps
-
Open Visual Studio Code with the project folder open.
-
Open
main.pyin the editor. -
Click the Run Button, the triangular play icon in the top-right corner of the editor.
-
VS Code opens a terminal, activates the project’s virtual environment, and runs the file. The output appears in the terminal.
-
Open Visual Studio Code with the project folder open.
-
Open the built-in terminal using the menu View → Terminal.
-
Make sure the terminal is in the project folder. The last folder name in the terminal prompt should match the project name. If it is not, navigate to it with
cd ProjectName.
-
Run the program with the command:
python main.py. -
The program output appears in the terminal directly below the run command.
🎯 Expected Outcome
The program output appears in the terminal directly below the run command.
⚠️ Mistakes
Running from the wrong folder
When running from the terminal, python main.py looks for main.py in the current folder. If the terminal is in a parent folder or elsewhere, the command fails with an error like can't open file 'main.py'. Use cd ProjectName to move into the folder that contains main.py, or use the play button, which always runs the open file.
Confusing python and python3
On Windows the command is usually python. On macOS and Linux it is python3, because python may point at an older version or be missing entirely. Use the command for the operating system, or use the play button, which picks the correct interpreter automatically.
📚 Resources
- Getting Started with Python in VS Code (external link), official tutorial
- Running Python code in VS Code (external link), official guide