How to include Hy code into a separate file, and then import that using Hy?

百般思念 提交于 2020-05-31 04:56:45

问题


I am just starting using Hy.

For example we have this Hy code:

(print "Hy, world!")

And we have two pieces of Python code. Piece one:

print("Some python code")

Piece two:

print("Some other python code")

How to include Hy code into a separate file, and then import that using Hy?

Please include all necessary code and instructions (where to put what and how to run) for Hy part and Python parts.


回答1:


Unfortunately, Hy's manual on this is a bit hidden (i.e. not part of the tutorial at the moment).

Anyway, you put your Hy code into a separate file and name it example.hy (or whatever):

(print "Hy, world!")

Inside your Python-script, you then simply import hy first, and afterwards you import example, just as you would with a Python module.

import hy
import example

The reason this works is because hy installs an import hook when you do import hy, which allows it to find hy-files, compile them and then import them just like any other Python module. Of course, you can also do stuff like this:

import hy
print("Some python code")
import example
print("Some other python code")


来源:https://stackoverflow.com/questions/61841891/how-to-include-hy-code-into-a-separate-file-and-then-import-that-using-hy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!