Skip to content

Instantly share code, notes, and snippets.

@LuisAlejandro
Last active April 15, 2020 04:20
Show Gist options
  • Save LuisAlejandro/89d29e6b4b0204d028a0e83b75afde6a to your computer and use it in GitHub Desktop.
Save LuisAlejandro/89d29e6b4b0204d028a0e83b75afde6a to your computer and use it in GitHub Desktop.
This script uninstalls all pip installed packages in the global scope. Must be run as root.
# This script uninstalls all pip installed packages in the global scope. Must be run as root.
for PYTHON in $( ls -1 /usr/local/lib/ ); do
if [ "$( echo ${PYTHON} | cut -c -6 )" == "python" ]; then
PYTHONVER="$( echo ${PYTHON} | cut -c 7,8,9 )"
PYTHONVERSHT="$( echo ${PYTHONVER} | cut -c 1 )"
if [ -x "/usr/local/bin/pip${PYTHONVER}" ] || [ -x "/usr/bin/pip${PYTHONVER}" ]; then
PIPBIN="pip${PYTHONVER}"
elif [ -x "/usr/local/bin/pip${PYTHONVERSHT}" ] || [ -x "/usr/bin/pip${PYTHONVERSHT}" ]; then
PIPBIN="pip${PYTHONVERSHT}"
else
echo "There's no pip binary available for python ${PYTHONVER}"
continue
fi
for PYTHONPKG in $( ls -1 /usr/local/lib/${PYTHON}/dist-packages/ | grep '[egg|dist]-info' ); do
PYTHONPKGNAME="$( echo ${PYTHONPKG} | awk -F- '{print $1}' )"
if [ "${PYTHONPKGNAME}" != "pip" ] && [ "${PYTHONPKGNAME}" != "setuptools" ] && [ "${PYTHONPKGNAME}" != "wheel" ]; then
${PIPBIN} uninstall -y ${PYTHONPKGNAME}
else
continue
fi
PYTHONLIST="${PYTHONPKGNAME} (python${PYTHONVER}) ${PYTHONLIST}"
done
fi
done
echo "Uninstalled packages: ${PYTHONLIST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment