Run a Python Project in Visual Studio Code

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.

📋 Steps

  1. Open Visual Studio Code with the project folder open.

  2. Open main.py in the editor.

  3. Click the Run Button, the triangular play icon in the top-right corner of the editor.

  4. VS Code opens a terminal, activates the project’s virtual environment, and runs the file. The output appears in the terminal.

  1. Open Visual Studio Code with the project folder open.

  2. Open the built-in terminal using the menu View → Terminal.

  3. 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.

  1. Run the program with the command: python main.py.

  2. 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