首页 > 解决方案 > R网状在第二个python文件中导入模块不起作用

问题描述

我在通过调用的 python 脚本中加载模块时遇到问题reticulate

我的文件结构基本上是这样的:

Rscript.R

library(reticulate)
py_run_file("python1.py")
python1.py

print("test1")
import python2 as p2
p2.run_test(123)
python2.py

def run_test(x):
    print("inside p2-run_test()")
    print(x)

现在,如果我正在运行Rscript.R,它可以正常工作,但是像这样更改python2.py(添加一个新功能):

python2.py

def run_test(x):
    print("inside p2-run_test()")
    print(x)

def run_test2(x):
    print("inside p2-run_test2()")
    print(x)

像这样python1.py(调用所述函数):

python1.py

print("test1")
import python2 as p2
p2.run_test2(123)

我收到以下错误:

test1
Error in py_run_file_impl(file, local, convert) : 
  AttributeError: 'module' object has no attribute 'run_test2'

有人可以在这里帮助我如何解决这个问题吗?

标签: pythonrpython-3.xpython-modulereticulate

解决方案


正如 nicola 在评论中所说,这可以通过重新加载导入的模块来解决:

然后,您想在编辑后重新加载 python 模块。请参见此处:如何将模块“重新导入”到 python,然后在导入后更改代码

那为我修好了。


推荐阅读