Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaging hy code with zipapp does not work #1774

Open
AlexanderWingard opened this issue May 1, 2019 · 9 comments
Open

Packaging hy code with zipapp does not work #1774

AlexanderWingard opened this issue May 1, 2019 · 9 comments
Labels

Comments

@AlexanderWingard
Copy link

I'm writing this ticket in response to this How to load Hy package in zipapp? stackoverflow question.

I have tried reproducing this on the latest master a38cdb7 using the files in this gist

It works fine when not using zipapp:

$ python3 hyz
hello hy

But after zipping:

$ python3 -m zipapp hyz
$ python3 hyz.pyz 
Traceback (most recent call last):
  File "/home/lex/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/lex/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "hyz.pyz/__main__.py", line 2, in <module>
ModuleNotFoundError: No module named 'hyz'
@Kodiologist Kodiologist added the bug label May 2, 2019
@Kodiologist
Copy link
Member

As a workaround, you can translate Hy files to Python first with hy2py.

@AlexanderWingard
Copy link
Author

Ah, I was experimenting with using hyc to produce pyc files and put them inside the zip but that is not the right approach?

@Kodiologist
Copy link
Member

I don't know. You can try it. Python has had ambivalent support for running bytecode without a corresponding source file.

@AlexanderWingard
Copy link
Author

Using hy2py worked for my code, however, I really wanted to use the same trick to bring in hy.contrib.hy-repr to be able to print hy data structures nicely but did not work well for those modules.

@Kodiologist
Copy link
Member

What do you mean, "did not work well"?

@AlexanderWingard
Copy link
Author

Sorry, actually the problem was not in hy-repr but actually from it importing hy.core.language, so when trying to hy2py that file it ends up in some import loop:

Traceback (most recent call last):
  File "/home/lex/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/lex/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "./tmp2.pyz/__main__.py", line 2, in <module>
  File "./tmp2.pyz/hello.py", line 3, in <module>
  File "./tmp2.pyz/hy/contrib/hy_repr.py", line 1, in <module>
  File "./tmp2.pyz/hy/core/language.py", line 2, in <module>
ImportError: cannot import name 'accumulate' from 'hy.core.language' (./tmp2.pyz/hy/core/language.py)

@allison-casey
Copy link
Contributor

allison-casey commented Mar 12, 2021

zipapp doesn't package dependencies (which Hy is one of) along with the code being packaged. I believe in this use case you'd have to use something like shiv. This could be a good thing to add to the documentation.

@Kodiologist
Copy link
Member

Kodiologist commented Sep 18, 2022

Here's a minimal example that follows zipapp's instructions on how to include dependencies. But it still doesn't work, so there's still a bug, presumably.

$ mkdir myapp
$ echo '(print "Hi from Hy.")' > myapp/foo.hy
$ echo 'import hy, foo' > myapp/__main__.py
$ python3 -m pip install hy --target myapp
…
$ python3 myapp
Hi from Hy.
$ python3 -m zipapp myapp
$ python3 myapp.pyz
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/myapp.pyz/__main__.py", line 1, in <module>
  File "/tmp/myapp.pyz/hy/__init__.py", line 15, in <module>
  File "/tmp/myapp.pyz/hy/importer.py", line 175, in _inject_builtins
  File "/tmp/myapp.pyz/hy/macros.py", line 310, in load_macros
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'hy.core.macros'

@Kodiologist
Copy link
Member

#2354 partly provides this: hy myapp.pyz with the above example works, but python3 myapp.pyz still doesn't, now with a much longer and more cryptic stack trace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants