You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/scripts/misc/get_package_requirements.py

40 lines
1.2 KiB

import pkg_resources
def get_package_versions(requirements_path, output_path):
try:
with open(requirements_path) as file:
requirements = file.readlines()
except FileNotFoundError:
print(f"Error: The file '{requirements_path}' was not found.")
return
package_versions = []
for requirement in requirements:
# Skip empty lines and comments
if (
requirement.strip() == ""
or requirement.strip().startswith("#")
):
continue
# Extract package name
package_name = requirement.split("==")[0].strip()
try:
version = pkg_resources.get_distribution(
package_name
).version
package_versions.append(f"{package_name}=={version}")
except pkg_resources.DistributionNotFound:
package_versions.append(f"{package_name}: not installed")
with open(output_path, "w") as file:
for package_version in package_versions:
file.write(package_version + "\n")
print(f"Versions written to {output_path}")
# Usage
get_package_versions("requirements.txt", "installed_versions.txt")