How Version Control and Build Numbering works in Python project
In Python projects, version control and build numbering are managed through various tools and conventions, typically involving version control systems (like Git) and specific practices for versioning.
Version Control in Python
Python projects commonly use Git for version control, where each commit represents a change in the codebase. Version control allows developers to track changes, collaborate effectively, and manage different versions of the software.
Build Numbering in Python
Unlike languages with more structured build systems like Java (Maven/Gradle) or .NET (MSBuild), Python projects often have more flexibility in managing build numbers. Here's how versioning and build numbering are typically handled in Python:
-
Versioning in
setup.py
: Python projects often define their version number in thesetup.py
file. This file is used by tools likepip
for package installation.# setup.py from setuptools import setup setup( name='myproject', version='1.0.0', # other setup configurations )
-
Semantic Versioning (SemVer): Python projects commonly follow Semantic Versioning (SemVer), which includes:
- MAJOR: Incremented for incompatible API changes.
- MINOR: Incremented for backward-compatible functionality.
- PATCH: Incremented for backward-compatible bug fixes.
-
Incrementing Versions: Versions can be incremented manually by editing the version string in
setup.py
or using automated tools.
Automating Versioning and Build Numbering
Python projects may use tools like bumpversion
to automate version increments. bumpversion
simplifies the process of updating version strings across various files (like setup.py
, __init__.py
, etc.) and commit/tagging changes in Git.
Using bumpversion
-
Installation: Install
bumpversion
via pip:pip install bumpversion
-
Configuring
.bumpversion.cfg
: Create a.bumpversion.cfg
file in the project root to specify the files containing version information:[bumpversion] current_version = 1.0.0 commit = True tag = True [bumpversion:file:setup.py] [bumpversion:file:myproject/__init__.py]
-
Command Usage:
- Run
bumpversion
with the desired part to increment (major
,minor
,patch
):bumpversion patch
- This will update the version in
setup.py
and__init__.py
, commit the changes, and tag the commit with the new version.
- Run
Example Workflow
Here’s a simplified example of version control and build numbering workflow in a Python project:
-
Initial Setup: Define the initial version in
setup.py
:# setup.py from setuptools import setup setup( name='myproject', version='1.0.0', # other setup configurations )
-
Increment Version: Update version manually in
setup.py
or usebumpversion
:bumpversion patch
-
Commit and Tag:
git commit -am "Bump version to 1.0.1" git tag -a v1.0.1 -m "Version 1.0.1"
-
Publishing: Push the changes to your Git repository:
git push origin master --tags