Set Up Visual Studio Code (VS Code) for Python

To write Python code, we will use Visual Studio Code (VS Code) – a modern, lightweight, open-source text editor. It makes development efficient and faster with features like syntax highlighting, autocomplete, error detection, and debugging all in one place.

Here is a step-by-step guide to set up Python in VS Code on Windows, macOS, or Linux:

Step 1: Install Python

First, make sure Python is already installed in your system using the following command:

python --version  # Windows
# or
py --version      # Windows
# or
python3 --version # macOS/Linux

If it returns a version number like Python 3.x.x (e.g. Python 3.13.7), it means Python is already installed on your system.

If it is not already installed, you can follow the guide “Download and Install Python on Windows, macOS, and Linux” to install Python.

Step 2: Install Visual Studio Code

Download and install Visual Studio Code (VS Code) for your operating system from https://code.visualstudio.com/.

Step 3: Install the Python Extension

  1. Open VS Code.
  2. Click the Extensions icon on the left Activity Bar (or press Ctrl+Shift+X for Windows/Linux or Cmd+Shift+X for Mac).
  3. Search for python.
  4. Find the official “Python” extension by Microsoft (it has the highest download count) and click Install.

Step 4: Select the Python Interpreter

An interpreter is the program that reads and executes your Python code. You need to tell VS Code which one to use.

  1. Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
  2. Type Python: Select Interpreter.
  3. A list of available Python interpreters will appear. Choose the one you want to use for this project (e.g. Python 3.13.7).

Step 5: Create and Run a Python File

  1. Create a new folder python_project on your Desktop for your project and open it in VS Code (File>Open Folder…).
  2. Create a new file(File>New Text File).
  3. Save it as app.py (File>Save) in your python_project folder. The .py extension is crucial.
  4. Copy and paste the following code.
    print("Hello from VS Code!")
  5. Click the Run button ▶ in the top right.
  6. You should see the output Hello from VS Code! in your terminal.

Set up a Virtual Environment (Recommended)

A virtual environment is a self-contained directory that holds a specific version of Python and all the packages your project needs. This prevents conflicts between projects.

  1. Open a new terminal in VS Code (Terminal>New Terminal).
  2. Create a virtual environment inside your project folder python_project from the terminal.
    # On macOS/Linux
    python3 -m venv .venv
    # On Windows
    python -m venv .venv
    This creates a .venv folder
  3. Activate it in VS Code.
    • Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
    • Type Python:Select Interpreter.
    • Choose the interpreter from the .venv folder that appears in the list (e.g., venv/bin/python on Mac/Linux or .\.venv\Scripts\python.exe on Windows).
  4. Activate the virtual environment in the integrated VS Code terminal.
    # For macOS/Linux
    source .venv/bin/activate
    # For Windows
    .\.venv\Scripts\activate

Once activated, the terminal prompt displays the environment name (e.g., venv or .venv) at the beginning of the line. You can now run Python commands and install packages, all isolated within the virtual environment.

New Approach from here:

Set up a Virtual Environment (Recommended)

  1. Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
  2. Type Python: Create Environment and hit Enter.
  3. Choose your environment type. A prompt will appear asking you to choose between Venv and Conda. Select Venv.
  4. Select a Python interpreter you want to use for this environment from the list (e.g., Python 3.13.7).
  5. Wait for the environment to be created. VS Code will create a virtual environment, typically in a folder named .venv inside your project directory.
  6. Select the new interpreter. VS Code will automatically select the interpreter from the new environment for your current workspace. If it doesn’t, you can manually select it:
    • Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
    • Type Python:Select Interpreter.
    • Choose the interpreter from the .venv folder that appears in the list (e.g., venv/bin/python on Mac/Linux or .\.venv\Scripts\python.exe on Windows).

VS Code will automatically select the interpreter from the new environment for your current workspace. To confirm, open a new terminal (Terminal>New Terminal) in VS Code, the virtual environment’s name will appear in the parentheses at the start of the command prompt (e.g., (.venv) C:\your_project>).