Bumpversion : Manage your Python Project version
Ever find difficulty in managing the versions of your python project in your production environment and the capture the versions in the version control system(vcs). If yes, you can manage the versions using bumpversion.
bumpversion handle semantic versioning for your application, library, or module in any language you want. Its good feature is that it makes updating all of the files that can have your version numbers hardcoded in, like VERSION and documentation version numbers embedded in the text. It also offers a simple means of noting semantically which portions should increase, for example.
bumpversion [major|minor|patch]
Semantic versioning
Semantic versioning provides a consistent way for developers to give meaning to their software release.
Let’s take an example:
Release : 4.2.1
4 -> Major : version when you make incompatible API changes
2 -> Minor : version when you add functionality in a backward-compatible manner
1 -> Patch : typically bug fix and other changes that do not impact how the code is used
you have to increment the major, minor and patch fixes according to the release you need in your project.
Let’s take an example:
First of all implement bumpversion and wheel
pip install --upgrade bumpversion
pip install wheel
Here I’m creating a package called calc.py
__init__.py : Consider calc.py as a package .It contains the version data which upgraded according to the incremental increrase in version
__version__ = '1.0.0'
calc.py : calculator python file (contains your python files). Also consider to keep your py project files.
class Calculator:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num1 - self.num2
def multiply(self):
return self.num1 * self.num2
def divide(self):
if self.num2 != 0:
return self.num1 / self.num2
else:
return "Cannot divide by zero."
main.py: Call calc.py
README.md : Description about your package
setup.py : The focal point of building, distributing, and installing modules with Distutils revolves around the setup script. Its primary function is to articulate the details of your module distribution to Distutils, ensuring that the various commands affecting your modules execute appropriately.
I have used some dummy data for setup file
from setuptools import setup,find_packages
# Project metadata
name = "pythonproject"
setup(
name=name,
version='1.0.0',
description="Dummy Project",
author="dUMMY",
author_email="your.email@example.com",
packages=find_packages(include=['calc', 'calc.*']),
)
setup.cfg : Here you have your config data regarding bumpversion
[bumpversion]
current_version = 1.0.0
commit = True
tag = True
[bumpversion:file:setup.py]
search = version='{current_version}'
replace = version='{new_version}'
[bumpversion:file:calc/__init__.py]
search = __version__ = '{current_version}'
replace = __version__ = '{new_version}'
Now run the below command which creates binary distribution and source distribution files.
python setup.py bdist_wheel sdist
After running you’ll get more files and folders added to project path
whl file inside dist is is a Python distribution format that allows for faster and easier installation of Python packages. It is a built distribution of a Python package, and contains pre-compiled bytecode and metadata.
SOURCES.txt : Contains details regarding your project files
Now the version is 1.0.0 in __init__.py. Now we’ll increase the version in patch,minor and major by the below code
patch
This will increase the version to 1.0.1
bumpversion --allow-dirty patch
minor
This will increase the version to 1.1.0
bumpversion minor
major
This will increase the version to 2.0.0
bumpversion major
If you want to change your version to a certain version for example : 1.1.1 use the below code
bumpversion --new-version 1.1.1 part setup.py
At all the levels the version change will happen in __init__.py, setup.py file.
Hence, here how you can implement bumpversion in your project. You can also push the changes in github which take trace of your versions.