DEV Community

0xkoji
0xkoji

Posted on

Create requirements.txt without pip freeze

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')

Enter fullscreen mode Exit fullscreen mode

Top comments (0)