【问题标题】:Importing .hy file with function definition in .py file and after calling function from .hy from python在 .py 文件中导入 .hy 文件并在 .py 文件中调用函数,然后从 python 调用 .hy 函数
【发布时间】:2020-05-17 19:09:24
【问题描述】:

我的.py 文件如下所示:

import hy
import example
foo2()

我的.hy 文件如下所示:

(defn foo2 [] (+ 1 1))

.hy 文件与.py 文件位于同一文件夹中。

如果我运行 .py 文件,我会收到错误:

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


问题:如果我想用 Python 从.py 文件中调用.hy 文件中定义的函数,如何更正上面的代码?




顺便说一句,这很好用。

.py 文件中:

import hy
import example

.hy 文件中:

(print "Hello, World")

【问题讨论】:

    标签: python python-3.x hy


    【解决方案1】:

    Python 无法在该上下文中解析名称 foo2。也许你的意思是from example import foo2?我假设您的 .hy 文件是您导入的 example 模块。

    【讨论】:

    • from example import foo2 解决了这个问题。谢谢。
    • 吉尔奇!你还活着!
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2016-12-21
    • 2023-03-12
    • 2012-02-07
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多