0

I use conda to create a Python 2.7 environment including the R package. If I open a Python session in a console, I can check that R is indeed installed with the Popen constructor:

$ python
>>> from subprocess import Popen, PIPE
>>> proc = Popen(["which", "R"], stdout=PIPE, stderr=PIPE)
>>> proc.wait()
0

where the 0 means it is installed. But if I try the same commands from within a Sublime Text 3 REPL running under the exact same Python environment, I get a 1.

Why is this and how can I fix it?

Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

1

You need to communicate:

proc = Popen(['which', 'python'], stdout=PIPE)
proc.communicate()

('/Users/Kelvin/virtualenvs/foo/bin/python\n', None)

wait just waits for the subprocess to complete and gives you the return code (which is 0 if its successful)

if you get a different error code (1 meaning it failed), I'd look into confirming your virtual environment. try sys.executable

Kelvin
  • 1,357
  • 2
  • 11
  • 22
  • In a Python session `proc.communicate()` returns: `('/home/gabriel/anaconda3/envs/asteca27/bin/R\n', '')`. In a REPL it returns `('', '')`. So I guess it is not detecting `R` installed in the `anaconda3` environment from the REPL, even with `communicate()`. – Gabriel Jul 15 '17 at 21:49
  • It might be that if you're doing it from your repl (which is it, spyder, jupyter, else?) that it isn't writing to stdout but back to whatever the repl is so its coming back blank? I'm not sure about that last part to be honest. – Kelvin Jul 16 '17 at 13:33
  • I'm using Sublime Text 3. Thanks anyway, `communicate()` is useful. – Gabriel Jul 16 '17 at 14:36