2014-06-21 6 views
8

Ho alcuni comportamenti imprevisti che eseguono lo stesso script da Bash e da RStudio.RStudio non carica tutti i moduli Python tramite la chiamata rPython

Si prega di considerare quanto segue. Ho una cartella "~/rpython" contenente due script:

# test1.R 

library(rPython) 

setwd("~/rpython") 

python.load("test1.py") 

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

print(sqrt(number)) 
print(string) 

e

# test1.py 

import random, nltk 

number = random.randint(1, 1000) 

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

posso chiamare il mio script R da Bash con Rscript test1.R, che restituisce come previsto

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

e se chiamo di nuovo produrrà un numero casuale diverso

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

Ma quando eseguo lo stesso script (test1.R) da RStudio le cose si fanno strane. Qui l'uscita

# 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 

Per qualche ragione quando chiamo lo script da RStudio, l'interprete Python non può individuare il modulo nltk (che sembra essere lo stesso con altri pip moduli installati), ma non ha alcun problema importando random .

+0

Si sta utilizzando un virtualenv o qualsiasi altra installazione complessa del percorso di installazione/libreria? Vedo [questa domanda precedente] (http://stackoverflow.com/questions/20337202/using-python-virtual-env-in-r) ... – BrenBarn

+0

No, per quanto ne so. – CptNemo

+0

Il comportamento di 'number' e' string' si verifica ancora anche se si esegue il codice in una nuova sessione R? – BrenBarn

risposta

6

Ho avuto anche questo problema. Il problema era che il mio terminale bash sembra chiamare un python diverso da quello che è Rstudio. Ho anche imparato che se stai solo cercando di chiamare Python.load() da rPython, probabilmente stai meglio con system() dalla libreria R di base.

  1. Scopri quale python sta chiamando il tuo terminale bash. Vai al tuo terminale bash ed esegui which python. Per me (OS X 10.11.5) era /usr/local/bin/python. Ora che conosciamo il percorso completo, possiamo chiamarlo esplicitamente e impedire a R di scegliere un'altra versione che potrebbe essere installata in qualche angolo della macchina.
  2. Utilizzare system() per chiamare i comandi di bash da R anziché python.load() e utilizzare il percorso completo per lo script. Usando il tuo nome script di esempio, e il mio esempio python, sarebbe system('/usr/local/bin/python /path/to/file/test.py1')

Sperare che aiuti!

+0

Come si chiama il python che si desidera utilizzare esplicitamente con rPython? – Melanie

+0

Questa domanda/risposta dovrebbe aiutare: http://stackoverflow.com/questions/25383030/rpython-using-wrong-python-installation-on-mac-osx –

+0

Penso che la tua risposta avrebbe dovuto suggerire un confronto del risultato bash a il risultato R da: 'system (" python --version ")'. Quindi descrivere come risolvere le discrepanze. –