如何在zipapp中加载Hy包?

时间:2019-01-23 07:51:50

标签: python hy zipapp

我有一个Python脚本(作为入口点),一个Hy库(只需添加两列)和一个输入数据文件:

$ cat add_cols/app.py 
import hy
import sys
from mylib import add2col

def main():
    inp = sys.argv[1]
    out = sys.argv[2]
    print(f'read file {inp}, and convert to file {out}')
    add2col(inp, out)

if __name__ == '__main__':
    main()

$ cat add_cols/mylib.hy 
(import [pandas :as pd])

(defn add2col [inpfile outfile]
  (setv inp (.read_csv pd inpfile))
  (assoc inp "cola"  (+ (get inp "cola") (get inp "colb")))
  (.to_csv inp outfile :index False))

$ cat demo.csv
cola,colb
1,2
1,2
1,2
1,2
1,2
1,2
1,2
1,2
1,2
1,2

我可以直接运行它并获得结果:

$ cd add_cols
$ python app.py ../demo.csv res.csv
read file ../demo.csv, and convert to file res.csv
$ cat res.csv
cola,colb
3,2
3,2
3,2
...

现在,我尝试以zipapp的身份运行它:

$ python -m zipapp add_cols -m 'app:main'
$ python add_cols.pyz demo.csv res.csv
Traceback (most recent call last):
  File "/home/leo/apps/miniconda3/envs/hylang/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/leo/apps/miniconda3/envs/hylang/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "add_cols_hy.pyz/__main__.py", line 2, in <module>
  File "add_cols_hy.pyz/app.py", line 3, in <module>
ModuleNotFoundError: No module named 'mylib'

为什么无法加载mylib? 如何在zipapp中加载Hy库?

环境:

  • Anaconda中的Python 3.6

  • Ubuntu 16.04

谢谢!

1 个答案:

答案 0 :(得分:0)

是的,这是一个错误。现在是Hy's issue #1774