To run a Python script in VS Code, open the .py file and click the Run Python File play button at the top right of the editor. VS Code opens a terminal and runs the file with the interpreter you selected, which needs the Python extension and a separate Python 3 install.
There are four ways to run a script, and each suits a different job: the play button for a quick run, the terminal for full control, Shift+Enter for a few lines, and F5 for debugging. This page covers all four, plus how to pick the interpreter and what to check when the wrong Python runs. If Python is not installed yet, start with how to install Python on Windows.
Set up VS Code for Python
According to the VS Code documentation, you need two things before a script will run from the editor:
- A Python 3 interpreter, installed separately. The Python extension does not include one.
- The VS Code Python extension (
ms-python.python). It installs the Python Debugger extension with it.
Open the folder that holds your code with File > Open Folder, or run code . from a terminal in that folder. Working from a folder rather than a single loose file makes interpreter detection and relative paths behave predictably. Create a file such as hello.py with the New File button in the Explorer and add one line:
print("Hello from VS Code")
Run a Python file with the play button
With hello.py open, click the Run Python File play button at the top right of the editor. VS Code opens the integrated terminal, runs the file, and leaves the output there. The same menu has a down arrow with extra choices, including debugging the file.
You can also right-click inside the editor and choose Run Python > Run Python File in Terminal. According to the VS Code documentation, this route saves the file first.
Example output in the terminal:
Hello from VS Code
If the play button is missing, the Python extension is not installed or not enabled, or the file is not recognized as Python. Check that the file name ends in .py and that the language shown in the Status Bar says Python.
Run a Python script from the integrated terminal
The terminal gives you the same control as running Python outside VS Code, including arguments. Open it with Terminal > New Terminal (or Ctrl+`) and type:
python hello.py
On Windows, py hello.py also works, because py is the Python launcher. On macOS and Linux the command is usually python3 hello.py. Arguments go after the file name, and the script reads them from sys.argv:
python report.py input.csv --verbose
For more on opening, splitting and configuring terminals, see the VS Code terminal guide.
Choose the Python interpreter
The interpreter you select decides which Python version runs your file and which installed packages it can import. To choose one, open the Command Palette (Ctrl+Shift+P) and run Python: Select Interpreter. You can also click the Python version in the Status Bar at the bottom of the window. The Command Palette guide explains the palette itself.
The VS Code documentation says that when you have not picked an interpreter, it checks workspace-local environments (.venv and venv) first, then global or system interpreters. By default the extension looks for folders named .venv anywhere in the workspace.
A virtual environment keeps one project's packages apart from another's. In VS Code you can create one with Python: Create Environment, choose Venv, and pick a base interpreter. To do it by hand, see Python virtual environments (venv).
Check which Python actually ran
When results look wrong, print the interpreter path from inside the script:
import sys
print(sys.executable)
print(sys.version)
Example output (the path will differ on your machine):
C:\work\demo\.venv\Scripts\python.exe
3.12.4 (main, Jun 6 2024) [version string abbreviated]
If the path points outside your project's .venv when you expected it inside, select the interpreter again and rerun.
Run only a selection or a single line
For trying out a few lines, select them and press Shift+Enter. Right-clicking and choosing Run Python > Run Selection/Line in Python Terminal does the same. Both send the code to a Python terminal. To open an interactive >>> prompt without running anything, run Python: Start Terminal REPL from the Command Palette.
This is useful while you build a script up in pieces, since variables defined in one run are still there in the next. A full file run starts a fresh interpreter each time, so nothing carries over.
Run and debug a script with F5
Set a breakpoint by clicking in the gutter next to a line number or pressing F9, then press F5. If no configuration exists yet, VS Code asks you to choose one: select Python File. The script pauses at your breakpoint, and you can step through it and inspect variables. The step-by-step version is in how to use breakpoints in VS Code.
To pass arguments or change the working setup, F5 reads a launch.json file in the .vscode folder. The documented configuration for the current file in the integrated terminal looks like this:
{
"name": "Python Debugger: Current File (Integrated Terminal)",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["input.csv", "--verbose"]
}
program is the file to run, and ${file} means the file open in the editor. console set to integratedTerminal keeps output in the VS Code terminal, which is also the default. args is optional: use "args": "${command:pickArgs}" to be asked for arguments at each launch. Another setting, justMyCode, is true by default and keeps the debugger inside your own code. Set it to false if you want to step into standard library code.
Which method should you use?
| Method | Best for | Arguments | Stops at breakpoints |
|---|---|---|---|
| Run Python File button | Quick run of the whole file | No | No |
Terminal (python file.py) | Arguments, other tools, scripting | Yes, on the command line | No |
| Shift+Enter | A few lines at a time | No | No |
| F5 (debugger) | Finding bugs | Yes, via args | Yes |
If the script will not run
- The terminal opens the Microsoft Store when you type
python. Windows has an app execution alias for Python that points to the Store. Install Python, or turn the alias off; see Python app execution aliases on Windows. ModuleNotFoundError. The selected interpreter does not have the package. Select the interpreter that belongs to your project, then install the package with that interpreter's pip, for examplepython -m pip install requests. Usingpython -m pipguarantees the package goes to the same Python that runs your file.- PowerShell blocks environment activation. If the terminal shows a script execution error when VS Code tries to activate the venv, the cause is the PowerShell execution policy. The venv article above explains the fix.
- The venv is not active in a terminal you already had open. VS Code activates the selected environment when it opens a new terminal. After changing the interpreter or the activation setting, close the terminal and open a new one. The setting is
python-envs.terminal.autoActivationType, with the valuescommand(the default),shellStartupandoff. - Nothing happens, or the wrong file runs. The play button runs the file in the active editor tab. Click into the file you want first.
Menu names and settings in the Python extension change between releases, so if a label here does not match what you see, search the Command Palette for Python to find its current name. This page reflects the VS Code documentation as of November 2026.
Frequently asked questions
- Do I need the Python extension to run a script in VS Code?
- Not to run it from the terminal: you can open the integrated terminal and type python script.py with only Python installed. You do need the Python extension (ms-python.python) for the Run Python File button, interpreter selection and the debugger. The extension does not include Python itself, so a Python 3 interpreter must be installed separately.
- How do I run only part of a script in VS Code?
- Select the lines and press Shift+Enter, or right-click and choose Run Python, then Run Selection/Line in Python Terminal. With nothing selected, Shift+Enter runs the current line. The code runs in a Python terminal that keeps its variables between runs.
- How do I pass command-line arguments to a script in VS Code?
- The simplest way is to run it from the terminal: python script.py first second. To pass arguments when you start from F5, add an args property to the configuration in launch.json, for example "args": ["--quiet", "input.txt"]. Setting "args" to "${command:pickArgs}" makes VS Code ask for them on each launch.
- Why does VS Code run a different Python than the one I expected?
- VS Code uses the interpreter chosen with Python: Select Interpreter, shown in the Status Bar. If you never chose one, it picks a workspace environment such as .venv first and a global interpreter after that. Select the one you want, then run the file again and check the version it prints.