0

Python CLI application display version using the --version argument. What is the right way to store that information ? Argparse has an argument for that

import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--version', action='version', version='%(prog)s 2.0')
parser.parse_args(['--version'])

But I am not convinced it is the right way to do it. I'm thinking about storing the version in a file so it can be maintained by the build pipeline but it should be protected so users can't modify it.

BeeShopRocks
  • 124
  • 7

2 Answers2

1

I also create a version.py file, but my content is even simpler:

version = 'develop'

The clue is, that this one-line-file is replaced in my pipeline with the actual version coming from git tags. So every version provided by a pipeline build will have something like version = "v1.0.23" in it. Then I can simply do an from version import version wherever I need it.

Example for gitlab ci:

  # set version
  - VERSION=$(git describe --tags)
  - echo "version = '$VERSION'" > version.py
creolo
  • 318
  • 1
  • 8
0

I have created a Version.py file and imported it to the main Code.if its dev + will add to version and if it is exe the + will be ignored.

The file Content sample:

import sys
VERSION = 'V' + '0' + '.' + '3'
if getattr(sys, 'frozen', False): #adding + for dev and Build for exe
    VERSION = VERSION + ' '
else:
    VERSION = VERSION + ' +'
Vignesh
  • 1,553
  • 1
  • 10
  • 25