|
| 1 | +# Best Practices for Specifying Dependency Versions |
| 2 | + |
| 3 | +The best practices for specifying dependency versions depend on |
| 4 | +whether you are maintaining an application or a library. |
| 5 | + |
| 6 | +## If you are maintaining an application |
| 7 | + |
| 8 | +There are two suggestions that you shall follow. |
| 9 | + |
| 10 | +1. Pinning dependencies in a production environment. |
| 11 | + |
| 12 | + This refers to explicitly declaring the exact version of your application's dependencies, |
| 13 | + including those of its dependencies (a.k.a. transitive dependencies). |
| 14 | + This ensures consistent and predictable deployments by preventing accidental upgrades that might introduce errors or regressions. |
| 15 | + |
| 16 | + To achieve this, the steps can be different, based on the tooling you choose. |
| 17 | + For example, if you are using the common `pip` tool, |
| 18 | + you shall specify the version like this `pip install PackageName==x.y.z`, |
| 19 | + where x.y.z is the latest stable version that you have tested with your app. |
| 20 | + |
| 21 | + Another trick is that you may start from a clean virtual environment, |
| 22 | + install all the direct dependencies using the method described above, |
| 23 | + and then run a `pip freeze > requirements.txt`. |
| 24 | + It will generate a requirements.txt file containing all your dependencies |
| 25 | + (including the trasitive dependencies) pinned to their current versions. |
| 26 | + Now you commit that requirements.txt. |
| 27 | + From now on, you can consistently recreate your production environment by |
| 28 | + `pip install -r requirements.txt`. |
| 29 | + |
| 30 | + You may also use advance dependency management tools to simplify the work flow. |
| 31 | + For example, |
| 32 | + [pip-tools](https://pypi.org/project/pip-tools). |
| 33 | + |
| 34 | +2. Keep the dependencies updated. |
| 35 | + |
| 36 | + The purpose of pinning dependencies in the production is not about |
| 37 | + sticking with old versions forever. |
| 38 | + It is about allowing you to control when to update what. |
| 39 | + In the long run, you are still recommended to keep the dependencies updated because: |
| 40 | + |
| 41 | + * The digital landscape is rife with threats. |
| 42 | + Outdated dependencies are prime targets for malicious actors. |
| 43 | + By staying updated, you fortify your project against known vulnerabilities |
| 44 | + and significantly reduce the risk of exploitation. |
| 45 | + * Software is rarely perfect. New versions of dependencies often include bug fixes, |
| 46 | + improving the stability and performance of your application. |
| 47 | + Ignoring updates means missing out on these enhancements. |
| 48 | + |
| 49 | + You shall maintain a non-production environment, |
| 50 | + periodically upgrade its dependencies to latest versions, test it out. |
| 51 | + If the test passes, you can update the requirements.txt accordingly, |
| 52 | + and deploy it to your production environment. |
| 53 | + |
| 54 | + <!-- |
| 55 | + The content above is equivalent to |
| 56 | + [section 2 of this document](https://www.geeksforgeeks.org/best-practices-for-managing-python-dependencies/#2-use-a-requirementstxt-file). |
| 57 | + --> |
| 58 | + |
| 59 | +## If you are maintaining a library |
| 60 | + |
| 61 | +If you are maintaining a library (meaning it will be used by its downstream applications), |
| 62 | +you shall not pin your dependencies. |
| 63 | +Instead, you shall declare your dependencies by range, such as `msal>=1.33, <2.0`. |
| 64 | +The lower bound is the smallest version that started to provide the API you are using, |
| 65 | +the upper bound is the version that is expected to contain breaking changes. |
| 66 | + |
0 commit comments