vasili111
vasili111

Reputation: 6930

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

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.

Upvotes: 1

Views: 247

Answers (1)

Tobias
Tobias

Reputation: 1371

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")

Upvotes: 2

Related Questions