通过网状结构在 R 中导入 pandas 时出错

Error while importing pandas in R via reticulate

我正在使用 R,我想使用我在 Python 中编写的函数,它需要导入 pandas。因此,我在 R 中使用以下代码:

library(reticulate)
reticulate::py_install("pandas", force = TRUE)

运行没有问题。另外,我已经在 Python 中安装了 pandas。然而,当我 运行 导入脚本时 pandas:

source("script_with_pandas.py")

我收到以下错误:

Error in py_run_file_impl(file, local, convert) : 
  ImportError: C extension: No module named 'pandas._libs.interval' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --force' to build the C extensions first.

知道如何解决这个问题吗?

尝试

reticulate::source_python("script_with_pandas.py")

但我很确定这是一个环境问题。如果您使用的是 RStudio >=v1.4,那么您可以转到工具 --> 全局选项 --> python 解释器并检查您使用的是哪一个,这可能是问题所在。除此之外,我认为这只是将 pandas 软件包安装到正确环境中的问题。