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.pyfile. This file is used by tools likepipfor 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.pyor 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
bumpversionvia pip:pip install bumpversion -
Configuring
.bumpversion.cfg: Create a.bumpversion.cfgfile 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
bumpversionwith the desired part to increment (major,minor,patch):bumpversion patch - This will update the version in
setup.pyand__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.pyor 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