Created
September 2, 2023 19:17
-
-
Save marksowell/61a797bf134e8b2fb7d67010b567669d to your computer and use it in GitHub Desktop.
Migrate pip packages when upgrading Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Open both files and read lines | |
with open("old-packages.txt", "r") as f: | |
old_packages = f.readlines() | |
with open("new-packages.txt", "r") as f: | |
new_packages = f.readlines() | |
# Convert lines to dictionary format for easy lookup and merging | |
old_dict = {pkg.split('==')[0]: pkg.split('==')[1].strip() for pkg in old_packages} | |
new_dict = {pkg.split('==')[0]: pkg.split('==')[1].strip() for pkg in new_packages} | |
# Merge dictionaries. This prioritizes entries in new_dict over old_dict. | |
merged_dict = {**old_dict, **new_dict} | |
# Convert back to list format | |
merged_packages = [f"{pkg}=={version}" for pkg, version in merged_dict.items()] | |
# Save merged package list | |
with open("merged-packages.txt", "w") as f: | |
for pkg in merged_packages: | |
f.write(f"{pkg}\n") | |
print("Merged package list saved to merged-packages.txt.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment