Skip to content

import tscompare fails with LookupError when setuptools_scm is installed (wheel has no _version.py) #44

Closed

Description

@hyanwong

I'm trying to run tscompare, and Claude reports: pip install tscompare followed by import tscompare crashes if setuptools_scm is installed in the same environment. In a clean venv (no setuptools_scm) it imports fine.

Reproduce

python -m pip install tscompare setuptools_scm
python -c "import tscompare"

Result (tscompare 0.2.1, Python 3.12, setuptools_scm installed)

  File ".../tscompare/provenance.py", line 25, in <module>
    from . import _version
ImportError: cannot import name '_version' from partially initialized module 'tscompare'
...
  File ".../tscompare/provenance.py", line 32, in <module>
    __version__ = get_version(root="..", relative_to=__file__)
  ...
LookupError: setuptools-scm was unable to detect version for .../site-packages.

Cause

The 0.2.1 wheel doesn't contain tscompare/_version.py (contents: __init__.py, methods.py, provenance.py). So in provenance.py, from . import _version raises ImportError, and the fallback runs:

except ImportError:  # pragma: nocover
    try:
        from setuptools_scm import get_version
        __version__ = get_version(root="..", relative_to=__file__)
    except ImportError:
        pass

When setuptools_scm is importable, get_version runs against site-packages, which has no git metadata, and raises LookupError. That isn't caught, so the whole package fails to import. Without setuptools_scm, the ImportError is swallowed and __version__ stays "undefined", which hides the problem.

setuptools_scm is commonly present (e.g. in environments with tsdate or tskit installed from source), so this will affect many users.

Suggested fixes

  1. Make sure _version.py is written into the wheel at build time (version_file / write_to in [tool.setuptools_scm]), so the fallback is never reached. This looks like the root cause.
  2. Make the fallback robust: catch LookupError as well as ImportError (or except Exception), or use importlib.metadata.version("tscompare") instead of calling setuptools_scm at runtime.

Workaround: install from a git clone (pip install -e ./tscompare), where setuptools_scm can read the version from git, or uninstall setuptools_scm.

I saw the same error with the 0.2.1 wheel from PyPI and with pip install git+https://github.com/tskit-dev/tscompare.git.

Activity

  1. jeromekelleher commented on Oct 28, 2026

    @jeromekelleher
    Member

    Is this just tscompare or does it happen with other tskit-dev packages like msprime too? It should be a standardised usage across projects.

  2. hyanwong commented on Oct 29, 2026

    @hyanwong
    MemberAuthor

    There is no error with msprime or tskit:

    # works with msprime, fails with tscompare
    conda create -n newenv python=3.11
    conda activate newenv
    python -m pip install msprime setuptools_scm                                              
    python -c "import msprime"
    
  3. jeromekelleher commented on Oct 29, 2026

    @jeromekelleher
    Member

    That's good. Well, tell Claude to do whatever msprime is doing to solve this, and in general to review other tskit-dev projects first before suggesting solutions de novo. It's worth adding this to your Claude.md.

  4. added 2 commits that reference this issue on Oct 29, 2026
    1228503
    f94979c
  5. hyanwong commented on Oct 29, 2026

    @hyanwong
    MemberAuthor

    Fixed in #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions