Create requirements.txt without pip freeze

kojikanao profile image Koji (he/him) ・1 min read

This is completely useless for people since we can use pip freeze > requirements.txt to create a requirements.txt lol

But here is the code to do that without pip freeze command

import pkg_resources
OUTPUT_FILE = 'requirements.txt'
with open(OUTPUT_FILE, mode='w') as f:
    for dist in pkg_resources.working_set:
        requirement = dist.project_name + '==' + dist.version
        f.write(requirement + '\n')


Editor guide