AD JB Toolbox 01 EN

Python Tutorial / Write and Run Python in VS Code

Running Python in VS Code requires both an editor setup and an interpreter selection. Open the project folder and choose its virtual environment so that running code and installing packages use the same Python.

Prepare the editor and project folder

This guide assumes that Python is installed and that your project has a .venv environment. VS Code is an editor; installing it does not replace installing an interpreter. In Extensions, locate Microsoft’s Python extension and verify its publisher before installation.

For the relevant APIs and platform requirements, see Write and Run Python in VS Code documentation.

Use File → Open Folder to open python-lab instead of opening only an individual file. This helps align the terminal’s working directory and relative file paths with the project root.

Select and verify the interpreter

  1. Open the Command Palette and run Python: Select Interpreter.
  2. Choose the Python executable in the project’s .venv folder, or enter its path if it is missing from the list.
  3. Open a new terminal and check the executable and pip location.
python -c "import sys; print(sys.executable)"
python -m pip --version

Expect .venv\Scripts\python.exe on Windows or .venv/bin/python on macOS and Ubuntu. An existing terminal may retain its previous environment after an interpreter change, which is why the new terminal check matters.

Run a complete script

Save the complete code below as expense.py in a separate practice folder, using UTF-8. Run python expense.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.

amounts = [3500, 8000, 2500]
total = sum(amounts)
print(f"total={total}")

Expected output:

total=14000

Compare normal execution with debugging

Save the file and use the Python extension’s Run Python File command. Place a breakpoint on the line after total is calculated, then start debugging the Python file. Inspect amounts and total in the variables pane and step through execution. The value in the debugger should agree with the printed total.

If an import fails after installation, compare the interpreter and pip paths. If a file cannot be opened, check the working directory and filename. If edited code seems unchanged, confirm that you saved it and ran the intended file. Avoid filenames such as json.py or csv.py, which can shadow standard-library modules.

Other extensions may provide run buttons with different execution settings. Start with the Python extension’s commands and the selected project terminal, then investigate any differences explicitly.

More in This Category
Python Tutorial / What Is Python? Features and Practical Uses

Python Tutorial / What Is Python? Features and Practical Uses

Understand Python’s role, distinguish source files from the interpreter and terminal, and run a complete expense-total example with a small follow-up experiment.

Python Tutorial / Build a First Program with Variables, Input and Output

Python Tutorial / Build a First Program with Variables, Input and Output

Read expense input, convert text to an integer and format a result. Understand assignment, comments, f-strings and the source of common conversion errors.

Python Tutorial / Set Up Python on macOS and Ubuntu

Python Tutorial / Set Up Python on macOS and Ubuntu

Prepare a Python development environment on macOS or Ubuntu without replacing system Python. Verify paths, create a virtual environment and test execution.

Python Tutorial / Create Virtual Environments and Manage Packages with pip

Python Tutorial / Create Virtual Environments and Manage Packages with pip

Use venv and python -m pip to separate projects, record dependencies and troubleshoot interpreter, package and activation problems.

Python Tutorial / Write and Run Python in VS Code

Python Tutorial / Write and Run Python in VS Code

Choose a virtual-environment interpreter in VS Code, run a complete Python script and use a breakpoint to check values while diagnosing environment mismatches.

Python Tutorial / Install Python on Windows and Verify the Interpreter

Python Tutorial / Install Python on Windows and Verify the Interpreter

Set up Python on Windows with the official Install Manager, create a project environment, and verify command resolution, versions and executable paths.