Skip to content

Virtual Environment Setup Guide

Status: Informational
Scope: Virtual environment setup for the project
Last Updated: December 24, 2025 18:58:31
Created: December 8, 2025 17:09:22

πŸ“‹ Overview

This project uses a leetcode Python virtual environment to isolate dependencies. Unit test scripts automatically use this virtual environment.


πŸ”§ Virtual Environment Setup

Windows

1. Create Virtual Environment

# From project root directory
python -m venv leetcode

2. Activate Virtual Environment

# PowerShell
leetcode\Scripts\Activate.ps1

# CMD
leetcode\Scripts\activate.bat

3. Install Dependencies

# Install basic dependencies
pip install -r requirements.txt

# Install test dependencies
pip install pytest pytest-cov

Linux / macOS

1. Create Virtual Environment

# From project root directory
python3 -m venv leetcode

2. Activate Virtual Environment

source leetcode/bin/activate

3. Install Dependencies

# Install basic dependencies
pip install -r requirements.txt

# Install test dependencies
pip install pytest pytest-cov

πŸ§ͺ Running Tests

Test scripts automatically use the virtual environment:

# Windows
cd .dev
run_tests.bat

# Linux/Mac
cd .dev
./run_tests.sh

Method 2: Manual Virtual Environment Usage

# Windows
leetcode\Scripts\activate
cd .dev
leetcode\..\Scripts\python.exe -m pytest tests -v

# Linux/Mac
source leetcode/bin/activate
cd .dev
python -m pytest tests -v

Method 3: Direct Virtual Environment Python Usage

No need to activate the virtual environment:

# Windows (from project root)
leetcode\Scripts\python.exe -m pytest .dev/tests -v

# Linux/Mac (from project root)
leetcode/bin/python -m pytest .dev/tests -v

πŸ” Verify Setup

Check if Virtual Environment Exists

# Windows
dir leetcode\Scripts\python.exe

# Linux/Mac
ls -la leetcode/bin/python

Check if pytest is Installed

# Windows
leetcode\Scripts\python.exe -m pytest --version

# Linux/Mac
leetcode/bin/python -m pytest --version

Check Installed Packages

# Windows
leetcode\Scripts\activate
pip list

# Linux/Mac
source leetcode/bin/activate
pip list

πŸ“¦ Virtual Environment Paths

Test scripts use the following paths:

Windows

  • Python executable: leetcode\Scripts\python.exe
  • Activation script: leetcode\Scripts\activate.bat (CMD) or leetcode\Scripts\Activate.ps1 (PowerShell)

Linux/Mac

  • Python executable: leetcode/bin/python
  • Activation script: leetcode/bin/activate

⚠️ Common Issues

Q1: Virtual Environment Not Found

Error Message:

[ERROR] Virtual environment not found: leetcode\Scripts\python.exe

Solution:

# Create virtual environment
python -m venv leetcode

# Activate and install dependencies
leetcode\Scripts\activate
pip install -r requirements.txt
pip install pytest pytest-cov

Q2: pytest Not Installed

Error Message:

[ERROR] pytest is not installed in virtual environment

Solution:

# Activate virtual environment
leetcode\Scripts\activate  # Windows
source leetcode/bin/activate  # Linux/Mac

# Install pytest
pip install pytest pytest-cov

Q3: Module 'runner' Not Found

Error Message:

ModuleNotFoundError: No module named 'runner'

Solution: Ensure you run tests from the project root directory, or use the test scripts.


πŸ”„ Updating Dependencies

Update Test Dependencies

# Activate virtual environment
leetcode\Scripts\activate  # Windows
source leetcode/bin/activate  # Linux/Mac

# Update packages
pip install --upgrade pytest pytest-cov

Update All Dependencies

# Activate virtual environment
leetcode\Scripts\activate  # Windows
source leetcode/bin/activate  # Linux/Mac

# Update all packages
pip install --upgrade -r requirements.txt

πŸ“ requirements.txt

Ensure requirements.txt includes test dependencies:

# LeetCode Practice Framework - Dependencies

# Required packages:
debugpy>=1.8.0          # Debug support for VS Code

# Testing packages:
pytest>=7.4.0           # Unit testing framework
pytest-cov>=4.1.0       # Coverage reporting

# Optional packages (for complexity estimation):
big-O>=0.10.0           # Time complexity estimation

# Documentation generation (MkDocs):
mkdocs>=1.6.0           # MkDocs static site generator
mkdocs-material>=9.5.0  # Material theme for MkDocs
mkdocs-minify-plugin>=0.7.0  # Minify HTML output
mkdocs-include-markdown-plugin>=7.0.0  # Include markdown files

🎯 Best Practices

  1. Use Virtual Environment: Always install dependencies in the virtual environment
  2. Use Test Scripts: Test scripts automatically handle the virtual environment
  3. Regular Updates: Regularly update dependencies to get the latest features and fixes
  4. Version Control: Do not add the leetcode/ folder to Git

πŸ“ž Need Help?

If you have questions, please refer to: - Contributors README - Maintainer Guide - Testing Documentation - Complete Testing Documentation - Project main README.md - Python Environment section


Test Maintainer: luffdev
Created Date: 2025-12-08