This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Windows batch script tool for safely resetting Python virtual environments. The tool automatically detects common virtual environment directories, uninstalls all packages, and reinstalls essential base packages (pip, setuptools, wheel).
Key characteristics:
- Single-file batch script (
env-reset.bat) - Bilingual interface (Chinese/English)
- UTF-8 encoded for international character support
- Safety-first design with multiple confirmation steps
The env-reset.bat script follows a state-machine pattern with labeled sections using batch goto statements:
- Initialization (lines 1-4): Enable delayed expansion, set UTF-8 encoding, change to script directory
- Virtual Environment Detection (lines 11-37): Checks for
.venv,venv,env,.envin order - Environment Display (
:ShowEnv,:ShowVersion): Shows activated venv details, setsSELECTED_PYTHON=python - Global Warning (
:NoVenvWarning): Strong warnings when no venv detected - Python Version Selection (
:SelectPythonVersion, lines 107-214): NEW in v1.1.0- Detects all Python installations using
where pythonandpy -0p - Deduplicates findings via
:CheckDuplicatesubroutine - Auto-selects if only one version found
- Prompts user selection for multiple versions
- Sets
SELECTED_PYTHONto chosen Python executable path
- Detects all Python installations using
- Package Enumeration (
:GetPackages): Usespip freezeto list packages - Uninstallation (lines 270+): Batch uninstall via temp file
- Base Package Installation (
:InstallBase): Reinstalls pip, setuptools, wheel
Critical implementation details:
setlocal EnableDelayedExpansionat line 2 enables delayed expansion for entire script- Virtual environment activation happens BEFORE
gototo avoid variable scope issues SELECTED_PYTHONvariable stores the Python interpreter path throughout script execution- All
pythonandpipcommands use!SELECTED_PYTHON!with delayed expansion - All pip operations use
"!SELECTED_PYTHON!" -m pipfor version-specific execution where pythonshows current Python path (first result only for cleanliness)- Global environment operations require typing "YES" (uppercase, exact match)
- Temp files:
temp_packages.txt(package list),temp_python_list.txt(detected Pythons),temp_python_list.txt.dedup(deduplicated)
Manual testing workflow:
- Create a test virtual environment:
python -m venv .venv - Install some packages:
.venv\Scripts\activate && pip install requests numpy - Run the script:
env-reset.bat - Verify only base packages remain:
pip list
Test scenarios to cover:
- Virtual environment with packages (normal case)
- Empty virtual environment (should skip to base package installation)
- No virtual environment with single Python installation (should auto-select)
- No virtual environment with multiple Python installations (should prompt for selection)
- Different venv names (
.venv,venv,env,.env) - Broken pip (error handling verification)
- Invalid user input during version selection (input validation)
Version selection testing:
- Test with only one Python in PATH: should auto-select without prompting
- Test with multiple Python versions: should display list and accept numeric input
- Test invalid selection input: should re-prompt with error message
- Test selection out of range: should re-prompt with error message
When modifying env-reset.bat:
- Preserve UTF-8 encoding (
chcp 65001) - Maintain bilingual output (Chinese primary, context for English)
- IMPORTANT: Always use
!SELECTED_PYTHON!for python/pip commands (delayed expansion) - Test all
gotolabel paths to avoid dead code - Use
errorlevelchecks after critical operations (pip, file operations) - Keep temp file cleanup in all code paths
- Validate changes don't break variable scoping
- When adding new Python version detection methods, update
:SelectPythonVersion - Ensure
SELECTED_PYTHONis set in both venv and global paths
Batch scripting gotchas in this codebase:
setlocal EnableDelayedExpansionis required at script start for!var!syntax- Variables set inside
called scripts may not persist - hence early detection logic for /floops: use!var!for variables modified within the loopif errorlevel 1means "if errorlevel >= 1" (not "if errorlevel == 1")>nul 2>&1suppresses both stdout and stderr/iflag makes string comparisons case-insensitive- When extracting tokens from
for /f,tokens=*gets entire line,tokens=1,2gets first two space-delimited tokens for %%x in (!LINE!)iterates over space-separated tokens, last assignment wins (used to get last token)- Always quote paths in delayed expansion:
"!SELECTED_PYTHON!"not!SELECTED_PYTHON!
env-reset.bat- Main executable script (372 lines, v1.2.0)README.md- Bilingual documentation with usage examplesCHANGELOG.md- Version history (currently v1.1.0)CLAUDE.md- This file, guidance for Claude CodeLICENSE- MIT license.gitignore- Standard git ignore patterns
Major version changes:
- v1.0.0 (197 lines): Initial release with basic venv reset
- v1.1.0 (333 lines): Added multi-Python version detection and selection (+136 lines)
This is a simple, single-purpose tool with no dependencies beyond Windows and Python 3.x.