Export pip packages list

Export pip packages list

python
python-module

Sometimes while I start with a new project, I end up playing around with different external packages which I don’t keep track of. To list all the pip packages, we can use pip list in the terminal. But it won’t generate a requirements.txt file. We can use pip freeze > requirements.txt to generate it but it also contains the fixed version of the packages. I don’t want that. So, here is a simple way to generate requirements.txt file that contains only the package name.

First install setuptools using pip install setuptools

import pkg_resources

with open('requirements.txt', 'w') as file:
    for package in pkg_resources.working_set:
        file.write(package.project_name + '\n')

Save the file and execute it. And it’s done. We now have a requirements.txt that contains the list of every packages that I installed.