-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper function to read __version__ #1316
Comments
There seems like a lot of overlap between that and |
As far as I understand, I'm not intending to do this in the proposed function. It's really just getting the value of the |
Oh I see what you mean, you want to get the version from the source without importing the module. I think probably it's not a great idea. The 7 different mechanisms you describe for single-sourcing a version all generate a |
I beg to differ. This is only true for some of the approaches. The mechanisms hold the original version information in different places:
Each time, the purpose is to distribute the information to
That's basically solution 6, and not necessarily a good solution (see the comment there). A major point of the proposed function is that you don't have to import the package to get the version information. |
MotivationMaybe it's helpful to state my motivation in a bit more detail. I want to:
To me defining the version in the source code (1, 3) seem to be the best solution for this unless you need/want extra tools (2, 7). 4 and 5 don't have an advantage compared to 1, 3 but the added difficulty that I have to somehow set The proposed function helps to make the value of |
Note you can already use something like |
No, the seven solutions all lead to the same outcome - that
For As for using it in |
Ok, drop
No. The bullet points just describe the motivation. They can be achieved with some of the 7 solutions. The function just streamlines these solutions so that one does not have to copy boilerplate code. |
I suppose. I personally don't think there's a huge amount of boilerplate code involved in a lot of these solutions, so I am -1 on creating and maintaining in |
I accept you point of view but don't share it. To me, it's actually little effort for a good payoff. When trying to find out how to do this correctly, I found a lot of users with the same question and a number of different answers (let alone the 7 in the guide). Apparently, this is something many people stumble over. setuptools could really help package maintainers here by providing a simple standard solution. I would be willing to provide a PR and I don't think it's much of a burden to maintain. Essentially I don't see why the function should need any future changes. It's really simple and self-contained. Of course, the decision is up to you. |
If there were a single standard solution there wouldn't be 7 items on this list. And it's really more than 7, because item 2 is "use a version manager that handles it for you". I personally think you get the best "value for money" from You always have the option of creating a new package that does exactly what you say. If it's very useful presumably it could become the standard way of finding versions, but it would basically be competing with all the options in 2 and 7. |
I've intentionally come to setuptools and not created a new package, because I think an additional dependency just for this function is not neccessary and it's actually quite the topic of setuptools. Additionally setuptools is basically available everywhere. To me, the list could be more opinionated (For simple cases use A. If you additionally need feature x use B. Don't use C unless you really need feature y). The items just stand there side by side and you have to figure out yourself, when to use what. - If there is interest I could propose a more targeted rewrite. Anyway thanks for the discussion. Keep up the great work! |
At one point, setuptools had built-in support for the most popular source code management system (Subversion). In fact, it was maintaining the relationship between SVN and Setuptools that drew me into contributing to the project in the first place. I'm actually quite pleased with the direction things have been going, decoupling the functionality but making it increasingly convenient for a project to opt in. The latest triumph is the addition of PEP 518 support to Pip 10 (which is available in beta today, just
I do share your motivations. And I should point out that if your motivation is to have the version in only one place and if you tag your source code with version numbers, that tag necessarily has to be the one place. Regarding the Sphinx docs conf, I've found a solution with which I'm very happy. See the conf.py which relies on jaraco.packaging.sphinx to load the config (version, author, name, url) from the package metadata. Just make sure This approach allows the version to be specified exactly once, atomically, by tagging a commit. And the version number cascades from that tag (and even increments on intermediate commits). The main downside I face with this whole approach is the presentation of the version in |
This, like other solutions, involves importing the module. Importing is generally not an option when the project has dependencies, as the dependencies will have to be installed first, which requires reading the metadata to determine the dependencies, which also involves reading the package's version, leading to a circular, um, dependency. |
I don't think this is generally true when all you need is to import That said, there's also #1359, which supports loading the version information directly from a file. |
I'm pleased to say this issue is addressed by importlib_metadata, slated to be So between the two options for defining a version for a project:
You have straightforward ways to define the version in the package metadata, and then with importlib_metadata, you have elegant ways to present that version at run time. |
FYI to anyone still interested in a function that reads |
This allows people who use setuptools to maintain project versions in a single place, for example as attributes in their Python source. Closes pypa#1316 Closes pypa/pip#6004 Closes pypa/packaging.python.org#571
For what's worth you can now use |
10 years later, and the stack overflow question on this is still full of misleading answers. The solutions here also require specific python versions. Very sad that this did not get considered. It is full of boilerplate code hacks to do something that should ideally be read_version(path) as a one liner. Most of the solutions have complicated regex, so the solutions are not simple. By the way, discussion in this issue was more helpful. So thank you to all who contributed. |
Maintaining a consistent version number across different parts of a package is a burden for authors. It may be needed in different places such as the package/module itself, setup.py or a sphinx conf.py.
The packaging guide describes a total of 7 different approaches.
I propose to support at least a few of them with a new
get_version()
function in setuptools.The basic implementation should support methods 1 (parse the file for a regexp) and 3 (execute the file in a separate context). IMO, these are the best approaches unless you have or need further functionality like extra tools (2) or VCS support (7).
The idea is to have
__version__
set in your source code and fetch that value in helper scripts like setup.py or conf.py with a single line of code.Example uses:
get_version('mypackage.__init__.py', __file__)
get_version('../mypackage._version.py, __file__, method='exec')
I figure this is a reasonable functionality to provide for setuptools. If there is interest, I can provide a PR.
The text was updated successfully, but these errors were encountered: