astropy / package-template

Template for packages that use Astropy. Maintainer: @astrofrog
http://docs.astropy.org/projects/package-template/en/latest/
Other
60 stars 63 forks source link

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

Closed lpsinger closed 1 year ago

lpsinger commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Yes, that would do it.

weaverba137 commented 4 years ago

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 commented 4 years ago

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

maxnoe commented 2 years ago

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 commented 1 year ago

https://github.com/astropy/package-template#deprecation-warning