pythonpython-3.xhy

How to include Hy code inside python code?


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 can we make something like this:

print("Some python code")
(print "Hy, world!")
print("Some other python code")

Please also include appropriate imports. I have not found right way to import Hy.


Solution

  • Per the manual:

    import hy
    
    print("Some python code")
    hy.eval(hy.read('(print "Hy, world!")'))
    print("Some other python code")
    

    Note that the Hy code is compiled at run-time. We'll probably never be able to implement embedding Hy code in a Python script so that it can be compiled at the same time the Python code is compiled. You can do the reverse and embed Python in Hy, though:

    (pys "print('Some python code')")
    (print "Hy, world!")
    (pys "print('Some other python code')")