A comprehensive script to clean up and standardize Python development environment on macOS using Homebrew.
This script helps you maintain a clean Python development environment by:
- Standardizing on Python 3.12
- Removing older/unnecessary Python versions
- Setting up proper pip configuration
- Configuring virtual environment defaults
- Setting up Python-specific git ignores
- Creating useful Python-related aliases and functions
- macOS
- Homebrew installed
- Zsh shell (default on modern macOS)
- Git (for global gitignore configuration)
- ✨ Single Python version management
- 🔒 Safe environment configuration
- 🧹 Cleanup of old Python versions
- 📝 Comprehensive logging
- 💾 Automatic backups of modified files
- ⚡ Useful shortcuts and functions for Python development
- 🛡️ Virtual environment enforcement
- 🎯 Git configuration for Python projects
- Clone this repository:
git clone https://github.com/yourusername/python-environment-cleanup.git
cd python-environment-cleanup- Make the script executable:
chmod +x cleanup_python.sh- Run the script:
./cleanup_python.sh- Uninstalls Python versions 3.10, 3.11, and 3.13 (if present)
- Ensures Python 3.12 is properly installed and linked
- Sets up pip configuration to require virtual environments
- Configures Python-specific global gitignore
- Updates shell configuration with useful aliases and functions
- Removes Python cache files (optional)
- Cleans up old pip installations
- Removes pipx if installed
Adds the following to your .zshrc:
- Python aliases for consistency
- Virtual environment shortcuts
- Project creation helpers
- Path configurations
alias venv='python3 -m venv venv'
alias activate='source venv/bin/activate'
alias create-venv='python3 -m venv venv && source venv/bin/activate'pynew projectname # Creates a new Python project with virtual environmentalias pip-install='pip install --user'- Creates backups of all modified files
- Logs all operations
- Requests confirmation before destructive operations
- Verifies installation success
The script creates:
- Backups of all modified configuration files in
~/.config/python_cleanup_backups_[timestamp] - A detailed log file of all operations in
python_cleanup_[timestamp].log
./cleanup_python.sh- Open a new terminal window
- Verify Python installation:
python3 --version # Should show Python 3.12.xpynew myprojectThis will:
- Create a new directory
myproject - Set up a virtual environment
- Activate the environment
- Upgrade pip to the latest version
The script modifies the following files (after backing them up):
~/.zshrc~/.config/pip/pip.conf~/.gitignore_global
- Homebrew not installed
brew not foundSolution: Install Homebrew first:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"- Permission denied
Permission denied: ./cleanup_python.shSolution: Make the script executable:
chmod +x cleanup_python.sh- Python 3.12 installation fails Solution: Try updating Homebrew:
brew update
brew doctorThe script creates backups of all modified files. To revert:
- Go to
~/.config/python_cleanup_backups_[timestamp] - Restore the desired backup files
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by Python development best practices
- Built for macOS and Homebrew users
- Community feedback and contributions