RStudio 不会通过 rPython 调用加载所有 Python 模块 [英] RStudio doesn't load all Python modules via rPython call

查看:46
本文介绍了RStudio 不会通过 rPython 调用加载所有 Python 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Bash 和 RStudio 中运行相同的脚本时出现了一些意外行为.

I have some unexpected behaviours running the same script from Bash and from within RStudio.

请考虑以下事项.我有一个文件夹 "~/rpython" 包含两个脚本:

Please consider the following. I have a folder "~/rpython" containing two scripts:

# test1.R

library(rPython)

setwd("~/rpython")

python.load("test1.py")

number <- python.get("number")
string <- python.get("string")

print(sqrt(number))
print(string)

# test1.py

import random, nltk

number = random.randint(1, 1000)

string = nltk.word_tokenize('home sweet home')

我可以使用 Rscript test1.R 从 Bash 调用我的 R 脚本,它按预期返回

I can call my R script from Bash with Rscript test1.R, which returns as expected

>> Loading required package: RJSONIO
>> [1] 13.0384
>> [1] "home"  "sweet" "home"

如果我再次调用它会产生一个不同的随机数

and if I call it again will produce a different random number

>> Loading required package: RJSONIO
>> [1] 7.211103
>> [1] "home"  "sweet" "home" 

但是当我从 RStudio 运行完全相同的脚本 (test1.R) 时,事情变得很奇怪.这里输出

But when I run the very same script (test1.R) from RStudio things get weird. Here the output

# test1.R
> 
> library(rPython)
Loading required package: RJSONIO
> 
> setwd("~/rpython")
> 
> python.load("test1.py")
Error in python.exec(code, get.exception) : No module named nltk
> 
> number <- python.get("number")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'number' is not defined
Error in python.get("number") : Variable not found
> string <- python.get("string")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'string' is not defined
Error in python.get("string") : Variable not found
> 
> print(sqrt(number))
Error in print(sqrt(number)) : object 'number' not found
> print(string)
Error in print(string) : object 'string' not found

由于某种原因,当我从 RStudio 调用脚本时,Python 解释器无法定位模块 nltk(这似乎与其他 pip 安装的模块相同) 但导入 random 没有问题.

For some reason when I call the script from RStudio, the Python interpreter can't locate the module nltk (it seems to be the same with other pip installed modules) but has no problem importing random.

推荐答案

我也遇到了这个问题.问题是我的 bash 终端似乎正在调用与 Rstudio 不同的 python.我还了解到,如果您只是想从 rPython 中调用 Python.load(),那么使用基本 R 库中的 system() 可能会更好.

I had this problem, too. The issue was that my bash terminal seems to be calling a different python than the one Rstudio is. I also learned that if you're only trying to call Python.load() from rPython, you're probably better off with system() from the base R library.

  1. 找出您的 bash 终端正在调用哪个 python.转到您的 bash 终端并运行 which python.对我来说(OS X 10.11.5)它是 /usr/local/bin/python.现在我们知道了完整路径,我们可以显式调用它并防止 R 选择可能安装在您机器某个角落的另一个版本.
  2. 使用 system() 从 R 而不是 python.load() 调用 bash 命令,并使用脚本的完整路径.使用您的示例脚本名称和我的示例 python 路径,它将是 system('/usr/local/bin/python/path/to/file/test.py1')
  1. Figure out which python your bash terminal is calling. Go to your bash terminal and run which python. For me (OS X 10.11.5) it was /usr/local/bin/python. Now that we know the full path, we can call it explicitly and prevent R from choosing another version that might be installed in some corner of your machine.
  2. Use system() to call bash commands from R instead of python.load(), and use the full path to your script. Using your example script name, and my example python path, it would be system('/usr/local/bin/python /path/to/file/test.py1')

希望有帮助!

这篇关于RStudio 不会通过 rPython 调用加载所有 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆