在 R 中无法使用网状结构从导入的 python 文件中获取函数
In R cannot get function from imported python file using reticulate
我想导入一个 python 文件,然后使用 python 文件中的函数;但它不起作用(它仅适用于 source_python)。应该是这样吗?
在名为 the_py_module.py 的 python 文件中包含此代码:
def f1():
return "f one"
def f2():
return "f two"
R 脚本
# Trying to import the python file, which appear to work:
reticulate::import("the_py_module")
给出这个输出:
模块(the_py_module)
# But when calling the function:
f1()
我收到错误提示:
f1() 错误:找不到函数“f1”
虽然这可以使用源 python 脚本。
reticulate::source_python("the_py_module.py")
f1()
尝试以下方法:
> library(reticulate)
> my_module <- import(the_py_module)
> my_module$f1()
[1] "f one"
或者,使用您的方法
> my_module_2 <- reticulate::import("the_py_module")
> my_module_2$f1()
[1] "f one"
我想导入一个 python 文件,然后使用 python 文件中的函数;但它不起作用(它仅适用于 source_python)。应该是这样吗?
在名为 the_py_module.py 的 python 文件中包含此代码:
def f1():
return "f one"
def f2():
return "f two"
R 脚本
# Trying to import the python file, which appear to work:
reticulate::import("the_py_module")
给出这个输出: 模块(the_py_module)
# But when calling the function:
f1()
我收到错误提示: f1() 错误:找不到函数“f1”
虽然这可以使用源 python 脚本。
reticulate::source_python("the_py_module.py")
f1()
尝试以下方法:
> library(reticulate)
> my_module <- import(the_py_module)
> my_module$f1()
[1] "f one"
或者,使用您的方法
> my_module_2 <- reticulate::import("the_py_module")
> my_module_2$f1()
[1] "f one"