In a previous post I explained how to setup VirtualEnv and start a new project. I however never explained the one important aspect of VirtualEnv. Saving a requirements file, a list of all installed libraries, is one of the must useful aspects. Allowing yourself or other developers to easily load up the environment you used, the same libraries, same versions. All without overwriting your changing the versions your using on your own system, or other projects.

Freezing

Let’s say your sitting in front of a newly created project, you just setup the environment, installed all the foreseeable libraries, setup the structure of your project, and create the repository and committed all your changes. Your missing one thing, a requirements file, a file outlining to yourself or anyone else that might work on your project exactly what libraries you have. Creating such a file really is not that difficult, deciding where you want to put it, that can be tricky. I always place my requirements file right at the root of my repository. To create this file just run the following command, either keep it as is to drop your file in the current working directory, or add a path to it.


pip freeze > requirements

 

You now have your very own requirements file setup specifically for your project. The only task left for you at this point, is to add it to your repository and commit the changes. If down the road you need to install new libraries, running this command and overwriting your file, is perfectly fine, just be sure to commit those changes also.

 

Unfreezing

If you are about to work on an existing project, and have this project checked out and ready to go. The first thing you will want to do is setup the environment for that project, and activate it.


virtualenv --no-site-packages --distribute [project_folder]

cd [project_folder]

source bin/activate

 

Afterwards enter the project folder, assuming the original developer used similar naming conventions and named there requirements file requirements, you can install all the required libraries.


pip install -U -r requirements

 

If the original developer didn’t use any kind of environment, and they just simply listed the requirements. You are still in a good spot, you can build the environment from here using pip to install all the packages you need then saving your own requirements file.