我尝试使用forecast在python中安装rpy2软件包:

from rpy2.robjects.packages import importr
from rpy2.robjects import r
fore = importr("forecast")


但是它给出了一些错误:

RRuntimeError Traceback (most recent call last)
<ipython-input-2-07185d7e0d3a> in <module>
      1 from rpy2.robjects.packages import importr
      2 from rpy2.robjects import r
----> 3 fore = importr("forecast")

~\Anaconda3\lib\site-packages\rpy2-2.9.4-py3.7-win-amd64.egg\rpy2\robjects\packages.py in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, symbol_r2python, symbol_check_after, data)
    451     if _package_has_namespace(rname,
    452                               _system_file(package = rname)):
--> 453         env = _get_namespace(rname)
    454         version = _get_namespace_version(rname)[0]
    455         exported_names = set(_get_namespace_exports(rname))

RRuntimeError: Error in loadNamespace(name) : there is no package called 'forecast'

最佳答案

当我尝试加载另一个尚未安装的软件包时,我发现了相同的错误消息。
例如,我尝试加载名为forecast的程序包。这是我的方法:


尝试打开R程序,并尝试也通过键入install.packages("forecast")手动安装。然后,您可以重新加载(重新启动)python脚本,尝试做同样的事情(加载包forecast
您可以从python手动安装要使用/加载的所有未解析的软件包。这是我的代码:
rpy2.robjects.r('install.packages("forecast")')


或者你可以像这样自动化

import rpy2.robjects as ro

package_name = "forecast"

try:
    pkg = importr(package_name)
except:
    ro.r(f'install.packages("{package_name}")')
    pkg = importr(package_name)
pkg


希望能帮助到你

关于python - RRuntimeError:loadNamespace(name)中的错误:没有名为“forecast”的软件包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55638109/

10-12 20:05