From 4ad5070325394c7bd4675ae08a97647d2dfa4809 Mon Sep 17 00:00:00 2001 From: BitMaster Admin Date: Thu, 3 Oct 2024 08:48:59 +0300 Subject: [PATCH] Add uninstall_python.sh --- uninstall_python.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 uninstall_python.sh diff --git a/uninstall_python.sh b/uninstall_python.sh new file mode 100644 index 0000000..e89aae7 --- /dev/null +++ b/uninstall_python.sh @@ -0,0 +1,61 @@ +#!/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!"