Home  Python   How version ...

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:

  1. Versioning in setup.py: Python projects often define their version number in the setup.py file. This file is used by tools like pip for package installation.

    # setup.py
    from setuptools import setup
    
    setup(
        name='myproject',
        version='1.0.0',
        # other setup configurations
    )
    
  2. 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.
  3. 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

  1. Installation: Install bumpversion via pip:

    pip install bumpversion
    
  2. 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]
    
  3. 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.

Example Workflow

Here’s a simplified example of version control and build numbering workflow in a Python project:

  1. 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
    )
    
  2. Increment Version: Update version manually in setup.py or use bumpversion:

    bumpversion patch
    
  3. Commit and Tag:

    git commit -am "Bump version to 1.0.1"
    git tag -a v1.0.1 -m "Version 1.0.1"
    
  4. Publishing: Push the changes to your Git repository:

    git push origin master --tags
    
Published on: Jul 01, 2024, 07:51 AM  
 

Comments

Add your comment