Search code examples
pythonpython-3.xhy

Importing .hy file with function definition in .py file and after calling function from .hy from python


My .py file looks like:

import hy
import example
foo2()

My .hy file looks like:

(defn foo2 [] (+ 1 1))

.hy file is in same folder as .py file.

If I run .py file I am getting error:

runfile('D:/del/hy2/untitled46.py', wdir='D:/del/hy2')
Reloaded modules: example
Traceback (most recent call last):

  File "<ipython-input-274-3982ada2f243>", line 1, in <module>
    runfile('D:/del/hy2/untitled46.py', wdir='D:/del/hy2')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/del/hy2/untitled46.py", line 3, in <module>
    foo2()

NameError: name 'foo2' is not defined


Question: How to correct my code above if I want to call function defined in .hy file from .py file with Python?




By the way this works fine.

In .py file:

import hy
import example

In .hy file:

(print "Hello, World")

Solution

  • Python can't resolve the name foo2 in that context. Maybe you meant from example import foo2? I'm assuming your .hy file is the example module you imported.