March 5, 2023

TIL How to Reset Python Environment

I recently had to recover my system python after a mistaken pip install command and I thought I’d share the script I used to do it.

This script will:

  1. Check if pip or pip3 is the correct command to use
  2. Run pip freeze to get a list of all installed packages and save it to a temporary file
  3. Read the temporary file line by line and uninstall each package
#!/bin/bash

# Check if pip or pip3 is the correct command to use
if command -v pip &> /dev/null; then
  PIP_CMD="pip"
elif command -v pip3 &> /dev/null; then
  PIP_CMD="pip3"
else
  echo "Error: pip or pip3 command not found."
  exit 1
fi

# Create a temporary file to store the list of installed packages
TMP_FILE=$(mktemp)

# Get a list of all installed packages and save it to the temporary file
$PIP_CMD freeze > "$TMP_FILE"

# Loop over each package and uninstall it
while read package; do
  echo "Uninstalling package: $package"
  $PIP_CMD uninstall -y $package
done < "$TMP_FILE"

# Remove the temporary file
rm "$TMP_FILE"

echo "All packages have been uninstalled."