pythonsetuptoolssetup.pypython-packagingsetuptools-scm

Python setuptools_scm get version from git tags


I am using project.toml file to package my module, I want to extract the version from git tag using setuptools_scm module.

When I run python setup.p y --version command it gives this output 0.0.1.post1.dev0. How will I get only 0.0.1 value and omit the .post.dev0 value?

Here is project.toml file settings:

[build-system]
requires = ["setuptools>=46.1.0", "setuptools_scm[toml]>=5"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
version_scheme = "no-guess-dev"
local_scheme="no-local-version"
write_to = "src/showme/version.py"
git_describe_command = "git describe --dirty --tags --long --match v* --first-parent"

[tool.setuptools.dynamic]
version = {attr = "showme.__version__"}

output:

 python setup.py --version
setuptools/config/pyprojecttoml.py:108: _BetaConfiguration: Support for `[tool.setuptools]` in `pyproject.toml` is still *beta*.
  warnings.warn(msg, _BetaConfiguration)
0.0.1.post1.dev0

Thanks


Solution

  • setuptools_scm out-of-the-box generates development and post-release versions.

    To generate a release version like 0.0.1, you can pass a callable into use_scm_version:

    # content of setup.py
    
    def my_version():
        from setuptools_scm.version import SEMVER_MINOR, guess_next_simple_semver, release_branch_semver_version
    
        def my_release_branch_semver_version(version):
            v = release_branch_semver_version(version)
            if v == version.format_next_version(guess_next_simple_semver, retain=SEMVER_MINOR):
                return version.format_next_version(guess_next_simple_semver, fmt="{guessed}", retain=SEMVER_MINOR)
            return v
    
        return {
            'version_scheme': my_release_branch_semver_version,
            'local_scheme': 'no-local-version',
        }
    
    
    setup(use_scm_version=my_version)
    

    Reference: https://github.com/pypa/setuptools_scm#importing-in-setuppy