首页 > 解决方案 > 从 R 调用 Python 代码

问题描述

我正在尝试从 R 调用我的 python 代码。这是我尝试过的简单代码。

import tensorflow as tf

a1=tf.constant(23)
b1=tf.constant(25)
s1=tf.Session()

with tf.Session() as s1:
    out=s1.run(a1+b1)
    print (out) 

使用 rPython (R-library) 我尝试调用这个函数。

library(rPython)
# Load/run the main Python script
python.load("/Desktop/add.py")

但是为此发生了错误。

Error in python.exec(code, get.exception) : 
  Traceback (most recent call last):
  File "/home/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
In addition: Warning message:
In readLines(file) :
  incomplete final line found on '/Desktop/add.py'

但是我已经在 R 中安装了 tensorflow 作为install_tensorflow()。我找不到问题。有谁知道如何解决这个问题?

标签: rpython-3.xrpython

解决方案


第一条警告消息表明您的 TensorFlow 文件未添加到指定目录。

最后的消息表明最后一行/Desktop/add.py不以行尾 (EOL) 字符(换行符 ( \n) 或回车+换行符 ( \r\n))结束。此消息的初衷是警告您该文件可能不完整;大多数数据文件都有一个 EOL 字符作为文件中的最后一个字符。

要解决这个问题:

  1. /Desktop/add.py在文本编辑器中打开
  2. 导航到文件的最后一行
  3. 将光标放在该行的末尾
  4. return
  5. 保存文件

推荐阅读