| name | manage-python-env |
| description | Python virtual environment management using uv for fast and reliable environment setup. Create, maintain, and manage Python virtual environments with dependency management. Use uv for environment creation, package installation, dependency resolution, and project configuration. Use when setting up new Python projects, managing dependencies, troubleshooting environment issues, or ensuring reproducible development environments. |
Python Environment Management Skill
This skill provides tools and workflows for managing Python virtual environments using uv, a fast Python package installer and resolver.
Quick Start
- Install uv: Ensure uv is installed on your system
- Create environment: Use uv to create a new virtual environment
- Install dependencies: Add packages from requirements.txt or pyproject.toml
- Manage environment: Update, freeze, or clean up dependencies
- Troubleshoot: Resolve common environment issues
Core Workflow
1. Environment Setup
uv init project-name
cd project-name
uv venv
.venv\Scripts\activate
source .venv/bin/activate
2. Dependency Management
uv add package-name
uv add "package-name>=1.0.0"
uv add "package-name[extra]"
uv pip install -r requirements.txt
uv add --dev pytest black
3. Project Configuration
uv pip freeze > requirements.txt
uv sync
uv update
uv update package-name
uv vs Traditional Tools
Advantages of uv
- Speed: 10-100x faster than pip
- Reliability: Better dependency resolution
- Unified tool: Replaces pip, venv, pip-tools
- Cross-platform: Consistent behavior across systems
- Modern features: Built-in virtual environments, lock files
Command Comparison
| Task | uv Command | Traditional Command |
|---|
| Create venv | uv venv | python -m venv .venv |
| Install package | uv add package | pip install package |
| Install dev package | uv add --dev package | pip install package |
| Freeze deps | uv pip freeze | pip freeze |
| Sync env | uv sync | pip install -r requirements.txt |
| Update package | uv update package | pip install --upgrade package |
Environment Types
Development Environment
uv venv
uv add --dev pytest black flake8 mypy
uv add pandas numpy matplotlib
uv sync
Production Environment
uv venv --python 3.11
uv add "package==1.2.3"
uv pip compile requirements.in -o requirements.txt
CI/CD Environment
uv venv --python 3.11
uv add --no-dev package-name
uv sync --frozen
Project Structure
Recommended Layout
project/
├── .venv/ # Virtual environment (gitignored)
├── src/ # Source code
├── tests/ # Test files
├── pyproject.toml # Project configuration
├── requirements.txt # Pinned dependencies
├── requirements-dev.txt # Development dependencies
└── .python-version # Python version specification
Configuration Files
pyproject.toml
[project]
name = "my-project"
version = "0.1.0"
description = "My project description"
requires-python = ">=3.8"
dependencies = [
"requests>=2.28.0",
"pandas>=1.5.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.0.0",
"black>=23.0.0",
"flake8>=6.0.0",
]
[tool.uv]
requirements.txt
# Pinned dependencies
requests==2.28.2
pandas==1.5.3
numpy==1.24.3
Common Workflows
New Project Setup
mkdir my-project && cd my-project
uv init
uv venv
source .venv/bin/activate
uv add requests pandas
uv add --dev pytest black
mkdir src tests
touch src/__init__.py tests/__init__.py
echo ".venv/" >> .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore
Existing Project Setup
git clone https://github.com/user/project.git
cd project
uv venv --python 3.11
uv sync
source .venv/bin/activate
Real-world example: The YouTube-SC project includes complete setup scripts (setup-environment.bat and setup-environment.sh) and a comprehensive requirements.txt file. See examples/youtube-sc/ for details.
Dependency Updates
uv update --outdated
uv update
uv update package-name
uv update --pre
Troubleshooting
Common Issues
1. Environment Activation Fails
Symptoms: source .venv/bin/activate doesn't work
Solutions:
ls -la .venv/
. .venv/bin/activate
.venv\Scripts\activate
2. Package Installation Failures
Symptoms: uv add package fails with errors
Solutions:
uv cache clean
uv add package -v
python --version
uv add "package>=1.0.0,<2.0.0"
3. Dependency Conflicts
Symptoms: uv sync fails with resolution errors
Solutions:
uv sync --resolution=highest
uv sync --resolution=lowest-direct
uv pip list
uv remove conflicting-package
4. Slow Package Installation
Symptoms: uv is slow (unusual)
Solutions:
uv config set global.index-url "https://pypi.tuna.tsinghua.edu.cn/simple"
uv pip download package -d ./packages
Advanced Features
Lock Files
uv pip compile requirements.in -o requirements.txt
uv sync --frozen
Multiple Python Versions
uv venv --python 3.11
uv venv --python 3.10
uv venv --python 3.9
uv python list
Environment Isolation
uv venv --isolated
uv venv --copies
Cross-Platform Compatibility
uv pip compile requirements.in --platform linux --platform macos --platform windows
uv add "package; sys_platform == 'linux'"
Integration with Other Tools
IDE Integration
VS Code: Add to settings.json:
{
"python.defaultInterpreterPath": ".venv/bin/python",
"terminal.integrated.env.windows": {
"VIRTUAL_ENV": "${workspaceFolder}/.venv"
}
}
PyCharm:
- File → Settings → Project → Python Interpreter
- Add → Existing environment → Select .venv/bin/python
Docker Integration
FROM python:3.11-slim
# Install uv
RUN pip install uv
# Copy project files
COPY . /app
WORKDIR /app
# Create virtual environment and install dependencies
RUN uv venv && uv sync --frozen
# Use the virtual environment
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "main.py"]
CI/CD Integration
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v3
- run: uv sync --frozen
- run: uv run pytest
Best Practices
1. Version Pinning
- Pin exact versions in production (
==)
- Use ranges in development (
>=)
- Maintain separate requirements files for dev/prod
2. Environment Management
- Keep
.venv in project directory (not global)
- Include
.venv in .gitignore
- Document Python version requirement
3. Dependency Hygiene
- Regularly update dependencies
- Remove unused packages
- Audit for security vulnerabilities
4. Reproducibility
- Use lock files for exact reproducibility
- Document environment setup in README
- Test across Python versions
Resources
- uv Documentation: See
references/uv-docs.md for complete uv reference
- Common Recipes: See
references/recipes.md for common environment setups
- Troubleshooting Guide: See
references/troubleshooting.md for solving common issues
- Migration Guide: See
references/migration.md for migrating from pip/venv
- Project Examples: See
examples/youtube-sc/ for real-world configuration files from the YouTube-SC project
When to Use This Skill
Use this skill when:
- Setting up new Python projects
- Managing dependencies for existing projects
- Troubleshooting environment issues
- Creating reproducible environments
- Migrating from pip/venv to uv
- Setting up CI/CD pipelines
- Managing multiple Python versions
- Ensuring cross-platform compatibility