Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

version.py template should not import setuptools_scm if package is installed from a distribution #461

Closed
lpsinger opened this issue May 9, 2020 · 7 comments

Comments

@lpsinger
Copy link
Contributor

lpsinger commented May 9, 2020

The version.py template (the VERSION_TEMPLATE string constant in setup.py) unconditionally attempts to import setuptools_scm:

VERSION_TEMPLATE = """
# Note that we need to fall back to the hard-coded version if either
# setuptools_scm can't be imported or setuptools_scm can't determine the
# version, so we catch the generic 'Exception'.
try:
    from setuptools_scm import get_version
    version = get_version(root='..', relative_to=__file__)
except Exception:
    version = '{version}'
""".lstrip()

setuptools_scm is an expensive import because it imports pkg_resources, which enumerates all installed packages (see pypa/setuptools#926). On my MacBook with an SSD, this is the main cost of importing any Astropy affiliated package, adding 0.3 seconds of overhead. I have seen import pkg_resources or import setuptools_scm take seconds or more on NFS filesystems on computing clusters. It should be avoided where possible.

@pllim
Copy link
Member

pllim commented May 9, 2020

Hmm. I never used the template thingy in my projects, so I am not sure what is the correct solution here. Looks like astropy core is using this too. Maybe @astrofrog or @Cadair can comment.

Thanks for bringing this to our attention!

@astrofrog
Copy link
Member

This was added because people wanted the version to be updated correctly without re-running pip when using an editable install, but I agree that we don't really want that for installed stable versions. Maybe we can do something simple like check if a git repository is present one level up before trying to import setuptools_scm?

@lpsinger
Copy link
Contributor Author

Yes, that would do it.

@weaverba137
Copy link
Member

Sorry if I'm missing something here, but once setuptools_scm sets the __version__ value, during install, would it still be imported? That is, in a production environment, with fixed package versions, do we need to worry about this? Or is the concern production/test environments where packages are reinstalled every time?

@weaverba137
Copy link
Member

@astrofrog, why not check to see whether __version__ is already set? Or does that create a circular situation?

@maxnoe
Copy link
Member

maxnoe commented Jun 29, 2022

I can open a PR here introducing the same system as the astropy main package now uses, we copied this to our projects as well and it works great but is a bit of a pain to setup.

It would thus be great to have it in the template.

You can see a stripped-down example here:
https://github.com/cta-observatory/project-template-python-pure/tree/main/src/project_template_python_pure

Note the version.py and _dev_version/__init__.py files and the corresponding settings in setup.cfg and MANIFEST.in

@pllim pllim added the won't fix: deprecated Template is deprecated here label Aug 11, 2023
@pllim
Copy link
Member

pllim commented Aug 11, 2023

@pllim pllim closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants