如何用setuptools_scm从Git标签提取纯主版本号(去除后缀)
The .post1.dev0 suffix you're seeing comes from setuptools_scm detecting commits made after your v0.0.1 Git tag. Even with version_scheme = "no-guess-dev", the tool still appends these suffixes to indicate changes beyond the tagged release. To strip them and keep only the base tag version, you can use a custom version scheme function.
Here's how to implement this:
1. Create a Custom Version Scheme Function
Make a new file at the root of your project (same directory as pyproject.toml) called version_scheme.py with the following code:
def simple_version(version): # Extract just the base version from the Git tag, ignoring post/dev suffixes return version.tag.version
This function takes the Version object provided by setuptools_scm and returns only the semantic version part of your Git tag (without any prefix like v or extra suffixes).
2. Update Your pyproject.toml
Modify the [tool.setuptools_scm] section to use your custom function instead of the default no-guess-dev scheme:
[build-system] requires = ["setuptools>=46.1.0", "setuptools_scm[toml]>=5"] build-backend = "setuptools.build_meta" [tool.setuptools_scm] version_scheme = "version_scheme.simple_version" # Use our custom function 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__"}
3. Test the Result
Run the version command again:
python setup.py --version
You should now see only 0.0.1 as the output, with no extra suffixes.
Alternative: Keep the Function Inside Your Package
If you prefer to store the custom function within your showme package, place it in src/showme/version_scheme.py and update the version_scheme path to:
version_scheme = "showme.version_scheme.simple_version"
This works as long as your src directory is properly configured as the package root (which it should be with your current setup).
内容的提问来源于stack exchange,提问作者sfgroups

