RRuntimeError:loadNamespace(name)中的错误:没有名为“ forecast”的包

时间:2019-04-11 17:24:41

标签: python rpy2

我尝试使用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'

1 个答案:

答案 0 :(得分:0)

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

  1. 也尝试通过键入install.packages("forecast")打开R程序并尝试手动安装。然后,您可以重新加载(重新启动)python脚本,尝试执行相同的操作(加载程序包forecast

  2. 您可以从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

希望有帮助