Web Development with Django Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Handling project dependencies with pip

The pip is the most convenient tool to install and manage Python packages. Besides installing the packages one by one, it is possible to define a list of packages that you want to install and pass it to the tool so that it deals with the list automatically.

You will need to have at least two different instances of your project: the development environment, where you create new features, and the public website environment that is usually called the production environment in a hosted server. Additionally, there might be development environments for other developers. Also, you may have a testing and staging environment in order to test the project locally and in a public website-like situation.

For good maintainability, you should be able to install the required Python modules for development, testing, staging, and production environments. Some of the modules will be shared and some of them will be specific. In this recipe, we will see how to organize the project dependencies and manage them with pip.

Getting ready

Before using this recipe, you need to have pip installed and a virtual environment activated. For more information on how to do this, read the Working with a virtual environment recipe.

How to do it…

Execute the following steps one by one to prepare pip requirements for your Django project:

  1. Let's go to your Django project that you have under version control and create the requirements directory with these text files: base.txt for shared modules, dev.txt for development environment, test.txt for testing environment, staging.txt for staging environment, and prod.txt for production.
  2. Edit base.txt and add the Python modules that are shared in all environments, line by line, for example:
    # base.txt
    Django==1.8
    djangorestframework
    -e git://github.com/omab/python-social-auth.git@6b1e301c79#egg=python-social-auth
    
  3. If the requirements of a specific environment are the same as in the base.txt, add the line including the base.txt in the requirements file of that environment, for example:
    # prod.txt
    -r base.txt
  4. If there are specific requirements for an environment, add them as shown in the following:
    # dev.txt
    -r base.txt
    django-debug-toolbar
    selenium
  5. Now, you can run the following command in order to install all the required dependencies for development environment (or analogous command for other environments), as follows:
    (myproject_env)$ pip install -r requirements/dev.txt
    

How it works…

The preceding command downloads and installs all your project dependencies from requirements/base.txt and requirements/dev.txt in your virtual environment. As you can see, you can specify a version of the module that you need for the Django framework and even directly install from a specific commit at the Git repository for the python-social-auth in our example. In practice, installing from a specific commit would rarely be useful, for instance, only when having third-party dependencies in your project with specific functionality that are not supported in the recent versions anymore.

When you have many dependencies in your project, it is good practice to stick to specific versions of the Python modules as you can then be sure that when you deploy your project or give it to a new developer, the integrity doesn't get broken and all the modules function without conflicts.

If you have already manually installed the project requirements with pip one by one, you can generate the requirements/base.txt file using the following command:

(myproject_env)$ pip freeze > requirements/base.txt

There's more…

If you want to keep things simple and are sure that, for all environments, you will be using the same dependencies, you can use just one file for your requirements named requirements.txt, by definition:

(myproject_env)$ pip freeze > requirements.txt

To install the modules in a new environment simply call the following command:

(myproject_env)$ pip install -r requirements.txt

Note

If you need to install a Python library from other version control system or local path, you can learn more about pip from the official documentation at http://pip-python3.readthedocs.org/en/latest/reference/pip_install.html.

See also

  • The Working with a virtual environment recipe
  • The Including external dependencies in your project recipe
  • The Configuring settings for development, testing, staging, and production environments recipe