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

seanballais

Sean Francis N. Ballais

Posted on April 14, 2018

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

So, I have the following Python code residing in the __init__.py file of my Python project which I autogenerated using PyScaffold.

from pkg_resources import get_distribution, DistributionNotFound

try:
    # Change here if project is renamed and does not equal the package name
    dist_name = 'mlfq-sim'
    __version__ = get_distribution(dist_name).version
except DistributionNotFound:  # pragma: no cover
    # Can't unit test this for now since I do not know how to temporarily
    # remove a distribution during runtime. Help. Also, is this even
    # worth it?
    __version__ = 'unknown'
Enter fullscreen mode Exit fullscreen mode

I get that it gets the version of my project. But how does it do it? Is there a benefit in using it compared to the one below?

__version__ = 'some version'
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
seanballais
Sean Francis N. Ballais

Posted on April 14, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related