DEV Community

Kevin Mack
Kevin Mack

Posted on • Originally published at welldocumentednerd.com on

Updating version numbers for Python Packages in Azure DevOps

So I did a previous post on how to create package libraries in Python, and I wanted to put in a post here on how to solve a problem I immediately identified.

If you look at the setup.py, you will see that the version number, and other details are very much hard coded into the file. This is concerning as it requires a manual step to go and update this before you can do a build / publish activity. And honestly, nowadays CI/CD is the way of the word.

So to resolve this, I built a script to have the automated build agent inject the version number created by the CI/CD tool. And that code is the following:


import fileinput
import sys

filename = sys.argv[1]
text_to_search = sys.argv[2]
replacement_text = sys.argv[3]
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(text_to_search, replacement_text), end='')

Enter fullscreen mode Exit fullscreen mode

I then updated my setup.py with the following:


name="packageName", 
    version="{{ __BuildNumber__ }}", 
    python_requires = '>=3.7',
    description="{{ __BuildReason__ }}", 

Enter fullscreen mode Exit fullscreen mode

And that’s it, from their you just trigger this tile and inject the new build number into the file.

Latest comments (0)