Embedding version in command-line program

Loris Bennett loris.bennett at fu-berlin.de
Tue Oct 20 03:26:45 EDT 2020


"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> Tony Flury <tony.flury at btinternet.com> writes:
>
>> On 07/10/2020 12:06, Loris Bennett wrote:
>>> Hi,
>>>
>>> I have written a program, which I can run on the command-line thus
>>>
>>>    mypyprog --version
>>>
>>> and the get the version, which is currently derived from a variable in
>>> the main module file.
>>>
>>> However, I also have the version in an __init__.py file and in a
>>> pyproject.toml (as I'm using poetry, otherwise this would be in
>>> setup.py).
>>>
>>> What's the best way of reducing these three places where the version is
>>> defined to a single one?
>>>
>>> Cheers,
>>>
>>> Loris
>
>> My top level package always has a version.py file which defines __version__,
>> __author__ etc. I am not sure if that helps in your .toml file though - is it
>> executed or does it have the ability to read files when it creates the
>> distributable ?
>
> The problem is that the .toml file is not by default part of the
> distribution, but AFAIU is intended to store the project metadata used
> to create the METADATA file in dist-info when the package is built.
> Thus, the version defined in the .toml file is not directly available to
> the module itself.
>
> It seems to me, as a python novice, that poetry's function 'version'
> needs to be extended.  One possibility is that it should not only bump
> the version in the .toml file, but also bump the version in a version
> file like 'version.py' as well.  Alternatively, the value of the key
> 'version' in the .toml file, if it is not the version itself,  could be
> path to a file, such as a 'version.py', which holds the version.
>
> If that sounds reasonable, I could suggest it to the developers of
> poetry.

This is a known issue with a solution using 'importlib.metadata' or
'importlib_metadata' described here:

  https://github.com/python-poetry/poetry/pull/2366#issuecomment-652418094

Cheers,

Loris
-- 
This signature is currently under construction.


More information about the Python-list mailing list