Skip to content

Instantly share code, notes, and snippets.

@seasonedgeek
Created February 2, 2020 07:16
Show Gist options
  • Save seasonedgeek/143a0f6d1a66fd9249af1c4ff1a8e0b8 to your computer and use it in GitHub Desktop.
Save seasonedgeek/143a0f6d1a66fd9249af1c4ff1a8e0b8 to your computer and use it in GitHub Desktop.
Write Versioned Python Packages to Requirements File for Uninstallation
import re
"""This script prepares a stream of text (captured during a Terminal session).
The text represents the named package installations of `pyttsx3` and its
113 versioned dependencies.
The captured stream of text (copy & paste) was saved to the `trash_pkgs.txt`
file. This script transforms its contents to specification and writes a
`requirements.txt` file. The requirements file is used by the command:
`python3 -m pip uninstall -r requirements.txt`
`pipdeptree` verifies that only the captured dependencies, mentioned above,
apply to the `pyttsx3` package installation."""
with open("trash_pkgs.txt", "r") as trash_file:
pkgs = trash_file.read()
trash_file.close()
# create requirements.txt file
req_file = open("requirements.txt", "wt")
# package names end with version number
p = re.compile(r"\-\d+\.\d+")
# split space delimited named packages to list
pkg_list = pkgs.split()
for pkg in pkg_list:
# modify version delimiter
verNum = p.findall(pkg)[0]
verDlm = verNum.replace("-", "==")
# replace '-6.1' with '==6.1'
pkgV = pkg.replace(verNum, verDlm)
# write replacement to requirements.txt
req_file.write(("{0}\n").format(pkgV))
req_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment