Skip to content
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

Docs: How to single source version number? #216

Open
fohrloop opened this issue Nov 17, 2020 · 4 comments
Open

Docs: How to single source version number? #216

fohrloop opened this issue Nov 17, 2020 · 4 comments

Comments

@fohrloop
Copy link
Contributor

Hi,

Thanks for making and maintaining this awesome package! I was about to create something similar, but was happily surprised there is already a package that does the job. I have a question about single sourcing the version number.

Currently, I use a common strategy: __version__.py inside mypackage folder, which has just

__version__="1.1.0"

And then, in the setup.py, I read the version using some regex. In application code, I read the version by just importing by from mypackage.__version__ import __version__. or similar. When using pynsist, I have to define a version number for the application in installer.cfg:Application.version.

What is the recommended way to single source version number in applications using pynsist? I suppose there are many ways to do it, but probably there is one is covers most use cases and/or is better than the others.

@takluyver
Copy link
Owner

Glad you like it!

At present, this isn't possible with a static configuration file. Larger projects often favour either creating the config file from a script as a build step, or scripting the build with Pynsist's Python API.

I tend to favour using tools like bump2version to update version numbers over having a strictly single source for it. But I am also open to retrieving the version number automatically (e.g. Flit does this for publishing Python packages). I lean towards doing so with static analysis rather than importing the package, as importing it can have side effects, needs dependencies to be installed, etc.

@albertogomcas
Copy link

albertogomcas commented Aug 23, 2022

As far as I know, a common pattern is to declare the version numbers in pyproject.toml (keeping it up to date with poetry or other tools)
Then in some file (typically mypackage/__init__.py):

from importlib import metadata

try:
    __version__ = metadata.version(__package__)
except metadata.PackageNotFoundError:
    __version__ = "0.0.0"

However, this does not work for pynsist generated apps (it never finds the metadata and thus defaults to 0.0.0), because the mypackage is not really installed (?) so the mypackage-xxx.dist-info folder is not created inside pkgs (unlike for the rest of dependencies).

@takluyver
Copy link
Owner

Pynsist is roughly designed so that you don't need to make a package of your application code (in the sense that pip etc. know about) before you can build an installer for it. So yes, it copies the application code without 'installing' it in a Python packaging sense (the relevant spec is Recording installed projects). This might be something to change, but it's probably a trickier change than it sounds.

I personally dislike packages calling importlib.metadata.version() on import, because it scans all installed packages, which can be really quite slow.

@albertogomcas
Copy link

Thanks for the hint that using importlib like this may be a performance issue, good to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants