r - 从 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()。我找不到问题。有谁知道如何解决这个问题?
解决方案
第一条警告消息表明您的 TensorFlow 文件未添加到指定目录。
最后的消息表明最后一行/Desktop/add.py
不以行尾 (EOL) 字符(换行符 ( \n
) 或回车+换行符 ( \r\n
))结束。此消息的初衷是警告您该文件可能不完整;大多数数据文件都有一个 EOL 字符作为文件中的最后一个字符。
要解决这个问题:
/Desktop/add.py
在文本编辑器中打开- 导航到文件的最后一行
- 将光标放在该行的末尾
- 按
return
- 保存文件
推荐阅读
- angular-material - 如何删除空间底部垫形式场
- c# - 在按钮单击事件中从另一个类 Person 调用方法 PresentPerson()
- c++ - 递归到达一个非空的二叉搜索树
- ffmpeg - 如何使用 FFmpeg 将所有 png 文件从特定数字转换为 gif
- python - 找不到真正的 FFmpeg 的安装(ffprobe 自带)
- vb.net - 将对象或数组转换为不带列名的 JSON
- javascript - 在销毁调用之前清空数组
- angular - Bootstrap 汉堡包在 Angular 6 项目中不起作用
- javascript - Javascript函数异步执行,我不希望它
- javascript - 如果四个按钮之一被按下,JavaScript 会做一些事情