I came across pip freeze and virtualenv a couple years ago, and I was blown away. pip freeze is a Python command that saves packages that were installed using pip in the virtual environment. I had always been scared of virtual environments, but once I learned how easy it was to manage my dependencies, I couldn’t stop. I felt like a senior developer making a virtualenv for all my projects. Ever since then my developer skills have increased manifold, and I found the perfect way to manage my Python projects. Or so I thought.   

What Is Pip Freeze?

Pip freeze is a command in Python that allows you to save packages that were installed using pip in the virtual environment. 

A few months into this fairy tale, I started facing issues when I would go back to my old projects. They would either stop running or the available dependencies would throw compatibility errors. I was perplexed because I thought I had done everything right. I had separated the projects and their dependencies by creating virtual environments, so why weren’t my old projects running? Turns out the very thing that made me a better Python developer was becoming my hurdle — pip freeze. This was because of the way sub-dependencies were handled using pip freeze.

 

How Pip Freeze Works

When I first started a new project and installed the libraries, I would run my favorite command:

pip freeze > requirements.txt

Here’s why it caused issues. Let’s say you install package A in your project, which might have sub-dependencies B, C and D. Now, your requirements.txt file with the above command would look like this:

A==1.0
B==2.0
C==1.4
D==1.2

Now say, the owner of library A releases a new version that uses a different version of library B and removes library C. Since B and C are already installed, pip freeze would pick it up automatically and dump them into the exact versions as they were originally installed. Now, in a project with hundreds of dependencies, your requirements file would become very problematic when you’d change libraries. You would have to identify all the sub-dependencies and delete them accordingly. In this example, if A was now removed from the project, you would still be stuck with B, C and D even though they were only installed because of A. Deleting each of them is a mammoth task and can be very annoying on large projects.

There are other many issues that stem out of this problem which can break your project any day in the future.

A tutorial on pip freeze. | Video: CodingWithMitch

More on Python: Python Dictionary and Dictionary Methods: A Guide

 

Pip Freeze vs. Pipreqs

I’m not just here with problems, I also have a solution. I found a library called pipreqs, which fixes all the above issues and is very user-friendly.

 

Why Is Pipreqs Better?

Here are reasons why switching to pipreqs is a better idea than using pip freeze for the requirements file.

 

1. “Pip freeze only saves the packages that were installed with pip install in the virtual environment,” according to the pipreqs documentation in PyPi.

pip freeze only installs those packages which were installed using the pip installcommand. However, pip is not the only python package manager. We can also use Chocolatey, Conda and setuptools, etc., which are not supported by pip freeze. To use them, we’d have to write them manually in the requirements.txt file. pipreqs, on the other hand, has no such restriction.

 

2. pip freeze saves all packages and dependencies in the environment including those that you don’t use in your current project

This is the biggest drawback of pip freeze. In a project, the dependencies constantly change and have to be added, updated and deleted. However, it’s a monumental task to achieve this using pip freeze because it dumps whatever is already installed in the environment. pipreqs, on the other hand, only puts those libraries in the requirements file which have been used in the project through imports. This is extremely powerful when you are trying to change the requirements file later.

 

3. pipreqs is extremely easy to use.

To install the library, run the following command 

$ pip install pipreqs

To generate a requirements.txt file, all you have to do is run the following command.

$ pipreqs

If the requirements.txt file already exists, then run the following command:

$ pipreqs --force

This generates a requirements.txt file in the home directory of your project. You can also provide a path if you want to save the file in a specific location.

$ pipreqs /home/project/location

There are other alternatives like pip-tools and poetry, as well, which you can check out.

More on Python: 10 Python Cheat Sheets Every Developer Should Know

 

Disadvantages of Pip Freeze

pip freeze might seem useful initially, but it can mess up your project for the following reasons:

  1. It dumps all the libraries installed in your project including dependencies and sub-dependencies in the requirements.txt file.
  2. It still misses out on the libraries that are not installed using pip.
  3. It doesn’t remove a library automatically if it isn’t being used in a project.

For the aforementioned reasons, it’s advisable to use pipreqs, a Python library that fixes all the above issues and is much easier to use.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us