You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is there no preferred approach to single-sourcing the project version? #182
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?
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).
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.
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.
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...)
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.
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...
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.
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.
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.
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.
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.
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.
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.
#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".
Activity
pfmoore commentedon Nov 9, 2015
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).
nchammas commentedon Nov 9, 2015
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.
pfmoore commentedon Nov 9, 2015
I don't know, it's not my objection...
qwcode commentedon Nov 9, 2015
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.
pfmoore commentedon Nov 9, 2015
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
execfileapproach is Python 2 only. Theexecversion needs anopen()but Python 2 doesn't support supplying an encoding toopen(), and the default encoding depends on the user's PC settings. The best approach is only using ASCII in yourversion.pyand 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...)
dstufft commentedon Nov 9, 2015
You could always just add
myproject/_versionwhich is just a text file, then have:and
dstufft commentedon Nov 9, 2015
Also,
codecs.openis likeopenexcept it supports an encoding parameter and works on 2 and 3.qwcode commentedon Nov 9, 2015
btw, this is already presented as one of the options... #4
Changaco commentedon Nov 26, 2015
Note: #190 proposes changes to the document that would affect this discussion.
Changaco commentedon Nov 26, 2015
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.
nchammas commentedon Nov 26, 2015
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:
xavfernandez commentedon Nov 26, 2015
Why option 3 and not any other one ? :-)
Note also that all these discussions relate to the use of distutils/setuptools as build tool.
nchammas commentedon Nov 26, 2015
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.
nchammas commentedon Nov 26, 2015
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.
xavfernandez commentedon Nov 26, 2015
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.
22 remaining items
ChrisBarker-NOAA commentedon Jul 13, 2023
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.
flying-sheep commentedon Jul 13, 2023
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.hynek commentedon Jul 13, 2023
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.flying-sheep commentedon Jul 13, 2023
Happy to oblige:
importlib.metadata.packages_distributions()gives you a mapping.wimglenn commentedon Jul 13, 2023
@hynek Is there a reason why you went with
importlib.metadata.metadata("stamina")["version"]instead ofimportlib.metadata.version("stamina")?hynek commentedon Jul 13, 2023
Three theories:
wimglenn commentedon Jul 20, 2023
I offer #1276 as an alternative to #1273.
sinoroc commentedon Jul 22, 2023
I feel like it is a matter of deciding what we want the scope of
packaging.python.orgto be. From one of the latestdiscuss.python.orgthread (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.wimglenn commentedon Jul 23, 2023
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.
sinoroc commentedon Jul 23, 2023
I also wonder about the possible confusion between distribution package and import package. Should
import something; print(something.__version__)always report the same value asimportlib.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.
webknjaz commentedon Sep 6, 2023
FTR I commented on this one if anybody from this thread wants to take a look.
ncoghlan commentedon Oct 15, 2024
#1612 resolves this by explaining that
pkg.__version__andimportlib.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".
ChrisBarker-NOAA commentedon Oct 15, 2024
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.