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.
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.
Execute the following steps one by one to prepare pip requirements for your Django project:
- 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. - Edit
base.txt
and add the Python modules that are shared in all environments, line by line, for example: - 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: - If there are specific requirements for an environment, add them as shown in the following:
- 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:
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:
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:
To install the modules in a new environment simply call the following command:
- 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