Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- def get_installed_version(package_name):
- output = subprocess.check_output(['pip', 'show', package_name]).decode('utf-8')
- lines = output.split('\n')
- for line in lines:
- if line.startswith('Version:'):
- return line.split(':')[1].strip()
- return None
- def update_requirements_file(file_path):
- with open(file_path, 'r') as f:
- lines = f.readlines()
- updated_lines = []
- for line in lines:
- package_name = line.strip().split('==')[0]
- installed_version = get_installed_version(package_name)
- if installed_version is not None:
- updated_line = f"{package_name}=={installed_version}\n"
- else:
- updated_line = line
- updated_lines.append(updated_line)
- with open(file_path, 'w') as f:
- f.writelines(updated_lines)
- if __name__ == "__main__":
- requirements_file = "requirements.txt" # Replace this with the path to your requirements.txt file
- update_requirements_file(requirements_file)
Advertisement
Add Comment
Please, Sign In to add comment