In the development of my Django project I have a different configuration for production and for development. Both have a common configuration and the settings of each environment go in their respective file.
But this means that they have different requirements. For example, in development I use the package django-debug-toolbar
and in production I don't. On the other hand, in production I use the package gunicorn
and in development I don't.
And just like the project configuration, there are packages that are common to both environments.
But all the packages, currently, are in the same requirements file that was created when I did:
$ pip freeze > requisitos.txt
Then...
How can I divide the requirements of my project, into common requirements, for development and for production.
...because I hope it's possible
The following is a fairly basic proposal to manage completely independent isolated environments, simple because it only requires:
In addition to being simple, it is fully compatible with both Linux, Windows and MAC. It is based on Virtualenv , this tool basically "creates" isolated local Python environments in specific folders and the idea is that one "activates" these environments according to their own needs, we could maintain an environment for development and another for testing, or create one new development to test new versions of a certain package or even a new version of Python, all this in the framework of a local folder without the need for administration privileges or to modify the system.
Let's see your situation. We already have the version of python that we are going to use in the project installed and running on the system, we have it
pip
installed and updated. Well, the first thing is going to be to install Virtualenv , simple:pip install virtualenv
, and we are ready to start.Typically, these "virtualenv" environments are created as subfolders of our project, but they can be anywhere on the system. Suppose our project is in
~/Proyectos/Mi-proyecto
, we are going to create an environment for development in Python 3x in that folder and we will call itvenv-desa
(the name is the least important), we will doWith this we have created a folder
~/Proyectos/Mi-proyecto/venv-desa
and in it we have installed: Python (3x) and some basic packagessetuptools, pkg_resources, pip, wheel
, nothing more and nothing less. Now we simply have to "activate" it, this process simply modifies thepath
and some other environment variables to point to ourvenv-desa
. Activation on Linux is done, standing in the root folder of the project:In Windows it changes a little nothing else
Regardless of the system the effects are the same:
On the one hand, it modified the "prompt" indicating the name of the virtual environment and on the other it modified the path, so that when we run any of the python tools and python itself, we do it from this new folder. Now we only have to install the libraries or packages that we are going to use for our development environment, simply with the command
pip install [paquete]
, everything that we install will end up in this new folder. Finally if we do wepip freeze > desa-requirements.txt
will have our list of requirements for our development environment, which will help us if we have to regenerate the environment. If we want to disable our environment, we just do# deactivate
.Now, we see that setting up a test environment
venv-test
will only vary in the packages that we install and finally:pip freeze > test-requirements.txt
. In the end we will have our two environmentsvenv-desa
withvenv-test
their own totally isolated packages that we can use alternately or even at the same time in different terminals. If we need to "regenerate" either of the two environments, on another computer or even OS, the steps are almost the same as what we have seen:O well
As I was saying, this is a very simple and above all clear alternative to understand, surely there are better and more automated tools and procedures, but this scheme, at least for me, has been very practical.
I found this answer on SOen but but lost the reference. Unfortunately, this functionality is not properly documented so it could go unnoticed.
As documented in Patricio Moracho 's excellent answer , using can create requirements files, but in this alternative, we avoid repeating packages in the files by making three different requirements files:
pip
base.txt
- which contains the packages common to the environments usedlocal.txt
- which in this example contain the packages that are only used in local developmentprod.txt
- contains the packages that are only used in production.base.txt file
This is a normal requirements file
local.txt file
The requirements file can have several commands, which are half explained in the documentation
pip
, including references to other requirements files, and this is where the magic happens:This file tells you to install the packages listed in first
base.txt
and then the ones below (in this case, just one).prod.txt file
Packages used in production are listed here.
Use
So, to install the packages in the development environment, do this:
And on the production server: