In most of the cases, we might need external packages for the development of a python program. These external packages are either available in pypi repository or available locally as archive files. Usually people just installs the packages directly in the python environment using pip command.
The pip command by default installs the package from the pypi repository. If we are not specifying the version, it selects the latest available version of that package supported by the python present in the environment (Python 2 or 3). Because of this nature, the pip command will always pick up the latest version of the packages. The packages may undergo drastic changes in newer releases. For example, an application developed with version X of a package may not work with the version Y of the same package. So simply noting down the package names itself will not help to manage the project. We need the list of all packages with the versions. Also manually installing the packages one by one is also a difficult task, because there can be several tens of packages within a single project.
The best practices for managing packages in a project are
- Use python Virtual Environment.
- Create a requirements.txt to maintain the package details.
The details on how to create and use virtual environment is explained in my previous post.
requirements.txt is a simple text file to maintain all the package dependencies with versions. A sample format is given below
Click==7.0 Flask==1.0.2 Flask-Cors==3.0.7 itsdangerous==1.1.0 Jinja2==2.10 MarkupSafe==1.1.0 six==1.12.0 Werkzeug==0.14.1
Packages can be installed using a single command
pip install -r requirements.txt
Packages in an environment can be captured in a requirements.txt file in one shot using the following command.
pip freeze > requirements.txt
This practice will help developers to manage the dependency list and easy code migration.