Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active May 17, 2021 08:03
Show Gist options
  • Save aahnik/b2ffb88b2d5d2ffa943b468b668b4722 to your computer and use it in GitHub Desktop.
Save aahnik/b2ffb88b2d5d2ffa943b468b668b4722 to your computer and use it in GitHub Desktop.
A script to uninstall all python packages via pip and clear cache. (For Unix based systems only. Ditch Windows today.)

Run this script directly

curl -Lks bit.ly/pipclear | python
#! /usr/bin/env python
import os
from typing import List
def run(*commands: str) -> None:
for cmd in commands:
try:
status = os.system(cmd)
if status != 0:
raise Exception(f"{cmd} returned non zero exit code")
except Exception as err:
print(err)
def ensure() -> None:
os.chdir(os.path.expanduser("~"))
run("pip --version", "python --version", "pwd")
def python_packages() -> List[str]:
run("pip freeze > python_packages")
with open("python_packages") as file:
packages = [package.strip() for package in file.readlines()]
return packages
def uninstall(packages: List[str]):
for package in packages:
run(f"pip uninstall {package} -y")
run("pip cache purge")
run("rm python_packages")
def main():
ensure()
packages = python_packages()
uninstall(packages)
if __name__ == "__main__":
print("MIT LICENSE\nCopyright (c) 2021 Aahnik Daw")
main()
# AAHNIK 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment