Python package testing: Circular import in a

I’m trying to update and enable tests for Python package pyerfa (link to the expression in my fork), and I’m encountering this issue:

__________________ ERROR collecting erfa/tests/ ___________________
ImportError while importing test module '/build/pyerfa-'.
Hint: make sure your test modules/packages have valid Python names.
/nix/store/lx8vhp4fxclp494svlfis3sb2g8z4l9h-python3-3.10.12/lib/python3.10/importlib/ in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
erfa/ in <module>
    from .version import version as __version__  # noqa
erfa/ in <module>
    from . import ufunc
E   ImportError: cannot import name 'ufunc' from partially initialized module 'erfa' (most likely due to a circular import) (/build/pyerfa-

The accused file from upstream is this. I don’t see anything unusual there… Does anyone with good Python eyes can help?

It’s been some time since I last worked with python, but this definitely looks like a circular import:

Looks like maybe the same issue as Problem with importing erfa and astropy or photutils in iPython/Spyder · Issue #92 · liberfa/pyerfa · GitHub?

I’m not sure off the top of my head. I see some template files in the repo (**%2F*.templ&type=code) so I guess one thread to pull on is making sure that those are actually getting rendered out as expected and into the expected locations during the build process?

I don’t see any non-standard invocations in the Nix expression, so we might be missing a step if they explicitly run it locally or in CI?

Thanks for trying to help @lelgenio ! Are you suggesting that in general, every time a package uses from . import foo or import .foo etc is doing it wrong? I may write an upstream patch that will fix it all… It’s just surprising to me that this is so abundant in this code.

It’s the weirdest thing… I replaced every . with erfa in all imports, and it sort of fixed the issue now, but I had also to add rm -rf erfa in the preCheck to make the build work, because it seems the python interpreter didn’t know whether it should import erfa from $out or from $PWD… This scenario seems rather common in Nixpkgs to me - a Python package naming both the python files’ directory the same as the module (the <> in import <>) - hence it’s so weird that I have to rm -rf erfa in preCheck

EDIT: Now a detailed investigation is available at: