i didn’t want the above to get to long - do that first!!
but then let’s get versioning setup for you using setuptools_scm - steps are below. Take this one step at a time and ask questions if things don’t make sense! it’s a small set of changes but i know it can be a bit confusing when we first work on this part!!
Versioning
The next thing i want to do is talk about how you handle versioning. I see that you have setuptools_Scm listed in your pyprioject.toml file and you have an action that pushes to pypi when you make a release! both are excellent.
I also see however, that you have versions that appear to be manually entered throughout your package.
If you use setuptools_scm then you can upon build, use the tags that you are creating to version your package. This means that you’ll never have to manually add a version number again! yay to no more potential for human error in versioning and a single point of truth for the version!
to do this you’ll want to add a few things to your setup. For reference you can use stravalib as i set the same thing up there!
Let’s get setuptools_Scm working for you here…
Step one - update your pyproject.toml file
we use a src/ layout so i’ll modify here given you are using a flat layout right now. but you can see how things work there if you wish.
add a section like this to your pyproject.toml file. Note that i adjusting the paths for you to NOT use src given your package uses a flat layout not a src layout.
[tool.setuptools_scm]
# Make sure setuptools uses version based on the last created tag
version_scheme = "post-release"
# Make sure scm doesn't use local scheme version for push to pypi
# (so there isn't a + in the version)
local_scheme = "no-local-version"
# This _version_generated.py file is a file that you'll never want to add to version control - so you'll want to add it to your gitignore file. but when you build your package, setuptools_Scm creates it. and it contains version information that you will pull into your package below.
write_to = "stravalib/_version_generated.py"
write_to_template = '__version__ = "v{version}"'
then in the project section of your pyproject, you set the version to be dynamic - see here for an example
[project]
name = "stravalib"
description = "A Python package that makes it easy to access and download data from the Strava V3 REST API."
keywords = ["strava", "running", "cycling", "athletes"]
readme = "README.md"
dynamic = ["version"]
Step two - update your init file to import the version value from the file setuptools_scm creates
To make this work at the package level so you can call __version__
on your package, you can add a small conditional to your init.py like so (Example here):
try:
from ._version_generated import __version__
except ImportError:
__version__ = "unreleased"
What this does is in the built version of your package, it will import that version_generated file and access the version that way. but if your working locally it will assign version as unreleased. There are a few ways to do this but i’ve found this to work.
you should now be able to build your package and it will recognize the version in that build. when you make a release to pypi via that action, the new tag number will become the version that pypi uses because of that dynamic setting in your pyproject.toml file.