Skip to content

Is there no preferred approach to single-sourcing the project version? #182

Closed

Description

@nchammas

There are many approaches offered in the current documentation, but no approach seems to be "recommended" or otherwise suggested as the preferred approach to take. For a new developer looking to package their project, this can be confusing.

Looking at a few of the projects I'm familiar with, it seems that approach 3 is common.

Set the value to a __version__ global variable in a dedicated module in your project (e.g. version.py), then have setup.py read and exec the value into a variable.

Would it make sense to present this approach as more than just one among many? Or is there really no preferred way to single-source the project version, even for new projects?

Activity

  1. pfmoore commented on Nov 9, 2015

    @pfmoore
    Member

    Typically any attempt to recommend a preferred solution is met with debate. There really doesn't seem to be a consensus.

    FWIW, if I recall the common objection to the approach you mention here is that it doesn't work properly if your project build directory isn't on sys.path (which it may not be in certain situations).

    The objections to the other options seem to me to be no stronger (but no weaker either).

  2. nchammas commented on Nov 9, 2015

    @nchammas
    Author

    FWIW, if I recall the common objection to the approach you mention here is that it doesn't work properly if your project build directory isn't on sys.path (which it may not be in certain situations).

    Is that a common occurrence? I'm not trying to start a debate about this now; I just ask as a total newcomer to packaging. :)

    I suppose if there really is no consensus, or even no approach that we can offer as "works for most people", then we can close this issue.

  3. pfmoore commented on Nov 9, 2015

    @pfmoore
    Member

    I don't know, it's not my objection...

  4. qwcode commented on Nov 9, 2015

    @qwcode
    Contributor

    It think approach #3 is becoming more common (I use it now btw), but it's hard for me to say what is globally "more common", w/o doing some kind of analysis of what's being posted to pypi these days.

    let's keep this issue open, and maybe #3 can become preferred at some point.

  5. pfmoore commented on Nov 9, 2015

    @pfmoore
    Member

    BTW, my explanation of the objection I've seen is wrong (if you exec the file,you don't need it on sys.path).

    One real issue is getting encodings right. The execfile approach is Python 2 only. The exec version needs an open() but Python 2 doesn't support supplying an encoding to open(), and the default encoding depends on the user's PC settings. The best approach is only using ASCII in your version.py and relying on the fact that nobody's going to have a default encoding that isn't ASCII-compatible. But all it takes is a non-ASCII character in a comment and the read gets a decoding error :-(

    But it's a corner case, and not likely to affect newcomers (um, unless they don't speak English as their native language, I guess...)

  6. dstufft commented on Nov 9, 2015

    @dstufft
    Member

    You could always just add myproject/_version which is just a text file, then have:

    # myproject/__init__.py
    import pkgutil
    
    __version__ = pkgutil.get_data("myproject", "_version").strip()

    and

    # setup.py
    import os.path
    
    with open(os.path.join(os.path.dirname(__file__), "myproject", "_version")) as fp:
        version = fp.read().strip()
    
    setup(
        ...
        version = version,
        ...
    )
  7. dstufft commented on Nov 9, 2015

    @dstufft
    Member

    Also, codecs.open is like open except it supports an encoding parameter and works on 2 and 3.

  8. qwcode commented on Nov 9, 2015

    @qwcode
    Contributor

    You could always just add myproject/_version which is just a text file

    btw, this is already presented as one of the options... #4

  9. Changaco commented on Nov 26, 2015

    @Changaco
    Contributor

    Note: #190 proposes changes to the document that would affect this discussion.

  10. Changaco commented on Nov 26, 2015

    @Changaco
    Contributor

    The problem I have with all the solutions currently listed in the document is that if your project uses Git tags to mark releases, then there isn't a single source of truth anymore: there's the version number in the code and the one in the tags.

  11. nchammas commented on Nov 26, 2015

    @nchammas
    Author

    That's correct, but I think that falls outside of the domain of what the PUG should cover. If you want your single-source version to be in Git, then you're coupling how Python versioning is done to something outside of Python. What about users who use Mercurial or SVN? Should the guide cover those as well?

    As far as the PUG is concerned, I think agreeing on the best approach to having a single source for the version across your Python code is what we should shoot for. Once we have that, it's easier to make additional recommendations on how to add an additional layer of indirection to source that version from outside of Python.

    So to provide an example, say the guide evolves to converge on option 3. From there, it's much easier to add tips on how to source that version in turn from a VCS.

    For example:

    Set the value to a __version__ global variable in a dedicated module in your project (e.g. version.py), then have setup.py read and exec the value into a variable.
    You can set __version__ automatically by querying your VCS. For example, in Git...

  12. xavfernandez commented on Nov 26, 2015

    @xavfernandez
    Contributor

    Why option 3 and not any other one ? :-)
    Note also that all these discussions relate to the use of distutils/setuptools as build tool.

  13. nchammas commented on Nov 26, 2015

    @nchammas
    Author

    Per the discussion earlier in this thread, option 3 seems to be the option that new projects are converging on. Do you observe otherwise?

    I think the PUG serves the community best when it recognizes already-existing conventions, or when it helps nudge the community towards emerging conventions and accelerates their adoption. I don't think anyone here is trying to invent something new and impose it on people by diktat.

    Personally, which option we go with is less important to me than the fact that we converge on a single "recommended" option. It's better for users when there are fewer choices on display for implementing such a basic pattern.

  14. nchammas commented on Nov 26, 2015

    @nchammas
    Author

    To expand a bit: The PUG is a guide, not an encyclopedia. Its purpose is to present common advice to users facing common problems, not to document every possible permutation that people have found useful.

    At least, that's how I see the PUG. Perhaps I've misunderstood its purpose though.

  15. xavfernandez commented on Nov 26, 2015

    @xavfernandez
    Contributor

    Per the discussion earlier in this thread, option 3 seems to be the option that new projects are converging on. Do you observe otherwise?

    I must admit I usually don't check the version hack used by the library I use but at work we tend to use something similar to pip: https://github.com/pypa/pip/blob/develop/setup.py#L34-L39 so that would be option 1.

  16. 22 remaining items

  17. ChrisBarker-NOAA commented on Jul 13, 2023

    @ChrisBarker-NOAA
    Contributor

    Exactly -- MTOWTDI -- which is the point of this thread -- no definitive recommendation.

    The question is what the Docs should say -- let's have that discussion in the PR.

  18. flying-sheep commented on Jul 13, 2023

    @flying-sheep
    Contributor

    turns out users really like __version__.

    I’m not sure that’s a good reason to keep it.

    importlib.metadata.version('distname') is the standardized way to do this and always works, while __version__ is just a convention that is often correctly followed, but not always.

  19. hynek commented on Jul 13, 2023

    @hynek
    Member

    see python-attrs/attrs#1136 for the concrete issue. As far as I understand, it's impossible to reason from an imported module to the package that installed it (happy to be corrected tho). And looks like pydoc looks at __version__ too.

  20. flying-sheep commented on Jul 13, 2023

    @flying-sheep
    Contributor

    it's impossible to reason from an imported module to the package that installed it (happy to be corrected tho)

    Happy to oblige: importlib.metadata.packages_distributions() gives you a mapping.

  21. wimglenn commented on Jul 13, 2023

    @wimglenn
    Contributor

    @hynek Is there a reason why you went with importlib.metadata.metadata("stamina")["version"] instead of importlib.metadata.version("stamina")?

  22. hynek commented on Jul 13, 2023

    @hynek
    Member

    Three theories:

    1. Copy paste.
    2. It's the first I found and it was good enough.
    3. In legacy projects I mapped all the data and it was easier to map dunders like this (see eg attrs)
  23. wimglenn commented on Jul 20, 2023

    @wimglenn
    Contributor

    I offer #1276 as an alternative to #1273.

  24. sinoroc commented on Jul 22, 2023

    @sinoroc
    Contributor

    I feel like it is a matter of deciding what we want the scope of packaging.python.org to be. From one of the latest discuss.python.org thread (a few months ago already), I was under the impression that the direction was towards minimizing the scope as much as possible to focus mostly on the packaging specifications and the things that are part of Python and its standard library (in other words the things that are under our/PyPA's control).

    So in this case, I guess it would be enough to mention importlib.metadata.version() and how to use it. The other techniques such as those involving build back-ends plugins would be out of scope. We could mention that those exist but without naming them in particular. If we name them then we have to ensure over time that these techniques and plugins still exist and are still up-to-date with the latest specifications, otherwise we are misleading users.

  25. wimglenn commented on Jul 23, 2023

    @wimglenn
    Contributor

    Right, the approaches shown in #1273 would be more at home in the setuptools docs. They're features quite specific to a setuptools-based build backend. The Python Packaging User Guide should strive not to be so closely wedded to setuptools.

  26. sinoroc commented on Jul 23, 2023

    @sinoroc
    Contributor

    I also wonder about the possible confusion between distribution package and import package. Should import something; print(something.__version__) always report the same value as importlib.metadata; print(importlib.metadata.version('Something')).

    Even in the cases where both are strictly the same (and importing the package is not too costly in terms of resources), aren't we being misleading by recommending to obtain the version string from the import package instead of from the installed distribution package metadata.

  27. webknjaz commented on Sep 6, 2023

    @webknjaz
    Member

    I offer #1276 as an alternative

    FTR I commented on this one if anybody from this thread wants to take a look.

  28. added
    type: question
    on Nov 5, 2023
  29. ncoghlan commented on Oct 15, 2024

    @ncoghlan
    Contributor

    #1612 resolves this by explaining that pkg.__version__ and importlib.metadata.version("pkg") are reporting the version of two different things (an import package or module and a distribution package) that happen to share a name.

    The old setuptools-specific guide is now just a HTTP redirect to the discussion page that mostly defers to "consult the documentation of your chosen build system".

  30. ChrisBarker-NOAA commented on Oct 15, 2024

    @ChrisBarker-NOAA
    Contributor

    two different things (an import package or module and a distribution package) that happen to share a name

    They may not have the same name -- one reason why both are useful.

    I'll take another look at #1612 and see if there's anymore clarification needed there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: question

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions