← Back to Module 0: Languages & Platforms

Visual Studio Code Guide

~45 min For Python users

Visual Studio Code (VS Code) is a free, powerful code editor that has become the most popular IDE in the world. It works with virtually any programming language and is my recommended editor for Python development.

Why VS Code?

If you're going to learn Python (Yay!), I recommend using VS Code for several reasons:

  • Free and open-source — No cost, no licensing hassles
  • Works with everything — Python, R, Stata syntax, Markdown, LaTeX, and more
  • Excellent Python support — With the Python extension, it rivals dedicated Python IDEs
  • Git integration — Version control built right in
  • AI coding assistants — GitHub Copilot and Claude Code integrate seamlessly
  • Huge extension ecosystem — Add functionality for any use case

Installing VS Code

  1. Download from code.visualstudio.com
  2. Run the installer (works on Windows, Mac, and Linux)
  3. Launch VS Code

The VS Code Interface

VS Code has a clean, customizable interface. The image below shows the main areas of the VS Code window. Hover over each numbered region to learn what it does:

Visual Studio Code Interface
1
2
3
4

Hover over a numbered region to learn about that part of VS Code.

Image source: VS Code Documentation

Note: If you are not seeing the terminal window, you can open it by going to the top menu and selecting View > Terminal or by pressing Ctrl + `.

Installing Extensions

VS Code's power comes from extensions. Here's how to install them:

  1. Click the Extensions icon in the Activity Bar (or press Ctrl+Shift+X)
  2. Search for the extension name
  3. Click Install

Essential Extensions for This Course

Note: all names contain links to the extension pages with more details and descriptions.

Extension What it does Required for
Python (by Microsoft) Python language support, IntelliSense, debugging Python development
Jupyter Run Jupyter notebooks inside VS Code Interactive Python
Pylance Fast, feature-rich language support for Python Better Python autocomplete
GitLens Enhanced Git integration and history viewing Version control
Start with Python Extension

At minimum, install the Python extension by Microsoft. It will automatically recommend Pylance as well. These two extensions transform VS Code into a powerful Python IDE.

Extensions for Other Languages

Extension What it does Use case
Stata Enhanced Syntax highlighting for Stata .do and .ado files Reading/editing Stata code
stataRun Run Stata code directly from VS Code (Mac only) Execute Stata from VS Code
R (by REditorSupport) R language support, syntax highlighting, code completion Working with R scripts
LaTeX Workshop Full LaTeX support: compiling, preview, autocomplete Writing papers and documents

AI Coding Assistants

AI assistants can dramatically speed up your coding by suggesting completions, explaining code, and helping debug errors. Here are the main options:

Extension What it does Cost
GitHub Copilot AI pair programmer that suggests code as you type Free for students (via GitHub Education)
Claude Code Chat with Claude AI directly in your editor Requires Anthropic account
Codeium Free AI code completion alternative Free forever
Continue Open-source AI assistant, works with various LLMs Free (bring your own API key)
A Note on AI Assistants

While AI coding assistants are incredibly useful, I recommend learning to code without them first. Understanding the fundamentals will help you evaluate and debug AI-generated code. We'll cover AI-assisted coding in more depth in ProTools ER2.

Productivity & easy time interpreting code

Extension What it does
Markdown All in One Shortcuts and preview for Markdown files (READMEs, notes)
Rainbow CSV Colorize columns in CSV files for easier reading
Excel Viewer View Excel and CSV files in a table format

Python Virtual Environments

Virtual environments are one of the most important concepts in Python development. If you skip this section, you will run into problems eventually. Trust me on this one.

What is a Virtual Environment?

Imagine your computer is an apartment building. Python is the building itself, and packages (like pandas, numpy, matplotlib) are furniture. Now imagine every project you work on is a different apartment in that building.

The Apartment Building Analogy

Without virtual environments: Every apartment shares the same furniture. If Project A needs a red couch (pandas version 1.5) but Project B needs a blue couch (pandas version 2.0), you have a problem. You can only have one couch in the building.

With virtual environments: Each apartment has its own furniture. Project A's apartment has a red couch, Project B's apartment has a blue couch. They don't interfere with each other.

In technical terms, a virtual environment is an isolated Python installation with its own set of packages. When you activate a virtual environment, any packages you install go into that environment only, not your system-wide Python.

Why Do You Need Virtual Environments?

Here are the real-world problems that virtual environments solve:

Problem Without Virtual Environments With Virtual Environments
Version conflicts Project A breaks when you update pandas for Project B Each project has its own pandas version
Reproducibility "It works on my machine" but not your colleague's Export exact package versions with requirements.txt
Clean uninstall Packages scattered across your system Delete the folder, everything's gone
System safety Risk breaking system Python tools System Python stays untouched
Real Story from the Trenches

I once spent two days debugging why a student's code wouldn't run. The problem? They had installed packages globally, and a system update had changed their Python version. All their packages were installed for Python 3.9, but the system was now running Python 3.11. A virtual environment would have prevented this entirely.

Creating a Virtual Environment

Python has a built-in tool called venv for creating virtual environments. Here's how to use it:

Step 1: Open Terminal in VS Code

Press Ctrl + ` (backtick) to open the integrated terminal.

Step 2: Navigate to Your Project Folder

# Example: navigate to your project
cd ~/Documents/protools_er1/my_project

Step 3: Create the Virtual Environment

# Create a virtual environment named 'venv'
python -m venv venv

# Or with a more descriptive name
python -m venv my_project_env
# Create a virtual environment named 'venv'
python3 -m venv venv

# Or with a more descriptive name
python3 -m venv my_project_env

This creates a new folder (called venv or whatever name you chose) containing a complete Python installation.

Step 4: Activate the Virtual Environment

# Activate the environment
.\venv\Scripts\Activate.ps1

# If you get an execution policy error, run this first:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Activate the environment
venv\Scripts\activate.bat
# Activate the environment
source venv/bin/activate

You'll know it worked when you see (venv) at the beginning of your terminal prompt:

(venv) C:\Users\yourname\my_project> _

Step 5: Install Packages

Now any packages you install will go into this virtual environment:

# Install packages (they go into the venv only)
pip install pandas numpy matplotlib statsmodels

# See what's installed in this environment
pip list

Step 6: Deactivate When Done

# Leave the virtual environment
deactivate

How to Check Which Environment You're In

There are several ways to verify your current Python environment:

# Method 1: Look at your prompt
# If you see (venv) at the start, you're in the venv

# Method 2: Check which Python is being used
which python     # Mac/Linux
where python     # Windows

# If it shows a path inside your project's venv folder, you're in the venv
# Example output: /Users/yourname/my_project/venv/bin/python
import sys
print(sys.executable)
# Shows the full path to the Python interpreter
# If it's inside your venv folder, you're in the venv

import sys
print(sys.prefix)
# Shows the base location of the Python installation
# Look at the bottom-left of VS Code
# You'll see something like: Python 3.11.5 ('venv': venv)

# Or press Ctrl+Shift+P and type "Python: Select Interpreter"
# This shows all available environments

Where is the Virtual Environment Stored?

The virtual environment is simply a folder in your project directory. Here's what's inside:

my_project/
├── venv/ ← The virtual environment folder
│ ├── bin/ (Mac/Linux) or Scripts/ (Windows)
│ │ ├── python ← The Python interpreter for this env
│ │ ├── pip ← Package installer for this env
│ │ └── activate ← Activation script
│ ├── lib/
│ │ └── python3.11/site-packages/ ← Installed packages live here
│ └── pyvenv.cfg ← Configuration file
├── my_script.py ← Your code
└── requirements.txt ← Package list for sharing
Don't commit venv to Git!

The venv folder can be hundreds of megabytes. Never commit it to Git. Instead, add it to your .gitignore file and share a requirements.txt instead. We'll cover this in Module 8: Git & GitHub.

Sharing Your Environment: requirements.txt

To share your project with others (or recreate it on a new computer), you save the list of installed packages:

# Save your current packages to a file
pip freeze > requirements.txt

# On a new computer, recreate the environment:
python -m venv venv
source venv/bin/activate  # or .\venv\Scripts\Activate.ps1 on Windows
pip install -r requirements.txt

The requirements.txt file looks like this:

pandas==2.1.0
numpy==1.25.2
matplotlib==3.7.2
statsmodels==0.14.0

VS Code and Virtual Environments

VS Code automatically detects virtual environments in your project. Here's how to ensure VS Code uses the right one:

  1. Open your project folder in VS Code
  2. Create and activate a virtual environment (as shown above)
  3. Press Ctrl + Shift + P and type Python: Select Interpreter
  4. Choose the interpreter that shows ('venv': venv) or your environment name

From now on, VS Code will use this environment for:

  • Running Python files (F5 or Ctrl+F5)
  • Running code in the terminal
  • IntelliSense and autocomplete
  • Jupyter notebooks

Common Virtual Environment Issues

"python" command not found

On Mac/Linux, try python3 instead of python. On Windows, make sure Python is added to your PATH during installation, or reinstall Python and check the "Add Python to PATH" box.

PowerShell execution policy error (Windows)

Run this command in PowerShell as Administrator:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
VS Code doesn't detect my virtual environment

Make sure:

  • The venv folder is inside your project folder (not somewhere else)
  • You've opened the project folder in VS Code (not just a file)
  • Try Ctrl+Shift+P → "Python: Select Interpreter" → "Enter interpreter path" and navigate to the Python executable inside your venv
Packages installed but "ModuleNotFoundError"

This usually means you installed packages in the wrong environment. Check:

  1. Is your terminal prompt showing (venv)?
  2. Is VS Code using the right interpreter? (Check bottom-left corner)
  3. Run pip list to see what's installed in the current environment

Further Learning

Videos and Documentation

Essential Keyboard Shortcuts

Shortcut (Windows/Linux) Shortcut (Mac) Action
Ctrl + ` Cmd + ` Toggle integrated terminal
Ctrl + Shift + P Cmd + Shift + P Command Palette (access everything)
Ctrl + P Cmd + P Quick Open (find files fast)
Ctrl + B Cmd + B Toggle sidebar
Ctrl + / Cmd + / Comment/uncomment line
Shift + Enter Shift + Enter Run selected Python code

Video Tutorials

Recommended Videos