I am getting the version of my package using this code:
import importlib.metadata
importlib.metadata.version("mypackage")
As documented in this question
I would like to avoid hardcoding “mypackage” in this code and instead retrieve the package name from somewhere. How can I do that?
Other info – I am using poetry to manage dependencies. The package name is in my pyproject.toml
file.
Option #1
Parse the __name__
variable. Something like this probably works: __name__.split(".")[0]
This seems like a bad idea.
Option 2 – Parse the pyproject.toml
file and grab the name from there.