Amistad
Amistad

Reputation: 7410

Managing contents of requirements.txt for a Python virtual environment

So I am creating a brand new Flask app from scratch. As all good developers do, my first step was to create a virtual environment.

The first thing I install in the virtual environment is Flask==0.11.1. Flask installs its following dependencies:

  • click==6.6
  • itsdangerous==0.24
  • Jinja2==2.8
  • MarkupSafe==0.23
  • Werkzeug==0.11.11
  • wheel==0.24.0

Now, I create a requirements.txt to ensure everyone cloning the repository has the same version of the libraries. However, my dilemma is this:

Upvotes: 19

Views: 61222

Answers (4)

Vishvajit Pathak
Vishvajit Pathak

Reputation: 3731

One good thing here is you are using virtualenv, which will make your task very easy.

  1. Activate virtualenv ($source path_to_virtualenv/bin/activate)

  2. Go to your project root directory

  3. Get all the packages along with dependencies in requirements.txt

    pip freeze > requirements.txt
    
  4. You don't have to worry about anything else apart from making sure next person installs the requirements recursively by following command

    pip install -r requirements.txt
    

Upvotes: 38

Deke
Deke

Reputation: 4659

If you only want to see what packages you have installed then just do
pip freeze.

but if you want all these packages in your requirement.txt, then do
pip freeze > requirements.txt

Upvotes: 1

Klaus D.
Klaus D.

Reputation: 14404

Both approaches are valid and work. But there is a little difference. When you enter all the dependencies in the requirements.txt you will be able to pin the versions of them. If you leave them out, there might be a later update and if Flask has something like Werkzeug>=0.11 in its dependencies, you will get a newer version of Werkzeug installed.

So it comes down to updates vs. defined environment. Whatever suits you better.

Upvotes: 7

hjpotter92
hjpotter92

Reputation: 80647

You can (from your active virtual environment) do the following

pip freeze > requirements.txt

which'll automatically take care of all libraries/modules available in your project.

The next developer would only have to issue:

pip install -r requirements.txt

Upvotes: 8

Related Questions