62 lines
1.8 KiB
Bash
62 lines
1.8 KiB
Bash
#!/bin/bash
|
|
|
|
# This script uninstalls the currently installed Python, pip, distutils, and related packages from the deadsnakes PPA
|
|
|
|
# Function to check for errors
|
|
check_error() {
|
|
if [ $? -ne 0 ]; then
|
|
echo "Error: $1"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Step 1: Detect the installed Python version
|
|
PYTHON_VERSION=$(python3 --version 2>/dev/null | awk '{print $2}' | cut -d. -f1,2)
|
|
|
|
if [ -z "$PYTHON_VERSION" ]; then
|
|
echo "No Python version detected."
|
|
exit 1
|
|
else
|
|
echo "Detected Python version: $PYTHON_VERSION"
|
|
fi
|
|
|
|
# Step 2: Uninstall the detected Python version
|
|
echo "Uninstalling Python $PYTHON_VERSION..."
|
|
sudo apt remove --purge -y python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python${PYTHON_VERSION}-venv python${PYTHON_VERSION}-distutils
|
|
check_error "Failed to uninstall Python $PYTHON_VERSION."
|
|
|
|
# Step 3: Uninstall pip
|
|
echo "Uninstalling pip..."
|
|
sudo apt remove --purge -y python3-pip
|
|
check_error "Failed to uninstall pip."
|
|
|
|
# Step 4: Remove the deadsnakes PPA
|
|
if grep -q "deadsnakes" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
|
|
echo "Removing the deadsnakes PPA..."
|
|
sudo add-apt-repository --remove ppa:deadsnakes/ppa -y
|
|
check_error "Failed to remove the deadsnakes PPA."
|
|
else
|
|
echo "No deadsnakes PPA found."
|
|
fi
|
|
|
|
# Step 5: Clean up remaining packages
|
|
echo "Cleaning up remaining packages and dependencies..."
|
|
sudo apt autoremove -y
|
|
check_error "Failed to autoremove packages."
|
|
|
|
# Step 6: Verify uninstallation
|
|
echo "Verifying Python and pip uninstallation..."
|
|
if command -v python3 &> /dev/null; then
|
|
echo "Python is still installed."
|
|
else
|
|
echo "Python has been successfully uninstalled."
|
|
fi
|
|
|
|
if command -v pip3 &> /dev/null; then
|
|
echo "pip3 is still installed."
|
|
else
|
|
echo "pip3 has been successfully uninstalled."
|
|
fi
|
|
|
|
echo "Uninstallation completed!"
|