hy

__init__.py for hy modules with relative imports


How do I require all functions in a hy module in an __init__.py? I tried to use a similar model as in the hyrule __init__.py, but am getting the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <lambda>
  File "/nix/store/73jg56dr425ycv9p746arzf13m52q2n3-python3-3.10.4/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/nix/store/6gf7zzwc1ipf4pcissjmi6p7na9l0awk-python3.10-oreo-1.0.0.0/lib/python3.10/site-packages/oreo/__init__.py", line 7, in <module>
    hy.macros.require('.oreo',
  File "/nix/store/p32f25fn29070pphsrfj3pyywqsv1gph-python3.10-hy-0.24.0/lib/python3.10/site-packages/hy/macros.py", line 241, in require
    source_module = import_module_from_string(source_module, target_module)
  File "/nix/store/p32f25fn29070pphsrfj3pyywqsv1gph-python3.10-hy-0.24.0/lib/python3.10/site-packages/hy/macros.py", line 165, in import_module_from_string
    return importlib.import_module(module_name, package)
  File "/nix/store/73jg56dr425ycv9p746arzf13m52q2n3-python3-3.10.4/lib/python3.10/importlib/__init__.py", line 121, in import_module
    raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.oreo'

This is what's in my __init__.py:

import hy
hy.macros.require('.oreo',
   # The Python equivalent of `(require oreo *)`
   None, assignments = 'ALL', prefix = '')
hy.macros.require_reader('.oreo', None, assignments = 'ALL')
from .oreo import *

This is my general directory structure:

.
├── oreo
│  ├── __init__.py
│  ├── oreo.hy
├── poetry.lock
├── setup.py
└── pyproject.toml

And this is my pyproject.toml:

[tool.poetry]
name = "oreo"
version = "1.0.0.0"
description = "The Stuffing for Other Functions!"
authors = ["sylvorg <jeet.ray@syvl.org>"]
license = "OREO"
include = [
    "oreo/**/*.py",
    "oreo/**/*.hy",
]

[tool.poetry.dependencies]
python = "^3.9"
addict = "*"
autoslot = "*"
click = "*"
hy = "^0.24.0"
hyrule = "*"
nixpkgs = "*"
more-itertools = "*"
rich = { git = "https://github.com/syvlorg/rich.git", branch = "master" }
toolz = "*"

[tool.poetry.dev-dependencies]
pytest = "^3.0"
poetry = "*"
poetry2setup = "*"
pytest-hy = "*"
pytest-randomly = "*"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Solution

  • The real answer is that hy.macros.require and hy.macros.require_reader are private functions, which could disappear or get backwards-incompatibly changed without notice. There's no official way to require macros from Python. Delegate to Hy instead: hy.eval(hy.read('(require hyrule.hy-init :macros * :readers *)')).