DEV Community

Discussion on: I am kind of baffled by this code in the `__init__.py` file in my Python project. How does this work internally?

Collapse
 
seanballais profile image
Sean Francis N. Ballais

I get the fact that it checks for dependencies (and their versions) on runtime. But I don't get why it would need to use get_distribution() to get the version of itself. Wouldn't __version__ = 'some_version' suffice?

Collapse
 
daveshawley profile image
dave-shawley

Technically it gets the version from the named distribution’s metadata - usually the version kwarg to setuptools.setup() in setup.py. This can be different if setup.py dynamically appends SCM information to the hard-coded version number. Take a look at github.com/dave-shawley/setupext-g... for an example.

Collapse
 
grahamlyons profile image
Graham Lyons

It would suffice. This would just avoid the duplication by only having it set in setup.py and reading the value from there.