Replacement for pkg_resources.get_distribution?

I have an app with a --version flag. I don’t normally use it, but today I tried it and it failed with a traceback complaining that pkg_resources.get_distribution wasn’t known. The argparse action called for my -v flag is:

# Thanks to Matt Wheeler for this clever hack...
# https://mail.python.org/pipermail/python-list/2022-November/908314.html
class VersionAction(argparse.Action):
    "special handler for -v/--version"
    def __call__(self, parser, namespace, values, option_string=None):
        print(pkg_resources.get_distribution("waybackcrawl").version)
        sys.exit(0)

I’ve never really paid attention to the setuptools API, and as you can see from the comment, I stole the VersionAction class from an old python-list post. What is the more moderne way to get a package’s version?

The modern equivalent would be importlib.metadata. I can’t test currently but importlib.metadata.version should work (not sure if any back ports may be needed)

Confirmed, thanks @tapetersen.