0

I have a Python program, which searches for an anomaly (First train, then test). Now I need to start this Python program from RStudio. I have read about system('python myfirstpythonfile.py'), but when I launch my Python program in this way I have import errors with numpy, scipy, etc.

How can I launch my Python program from RStudio?

mfitzp
  • 15,275
  • 7
  • 50
  • 70
Parfi
  • 297
  • 1
  • 4
  • 6
  • By "problems with `numpy`" do you mean it can't import it? – mfitzp May 19 '16 at 00:25
  • Please specify what those *problems* are. As a test, run same line in your command line interpreter - Bash/PowerShell and see the output. – Parfait May 19 '16 at 00:57
  • @Parfi can you post the error messages you get (e.g. if you get an `ImportError` as suspected)? Then your question can be re-opened. – mfitzp May 19 '16 at 14:57

1 Answers1

-1

Having problems importing numpy or scipy suggests that your script is not running in the correct Python environment. It is possible to install multiple versions of Python on a computer, and which one is run when you type python is determined by the PATH setting. It may be that when RStudio executes your script (via python myfirstpythonfile.py) it is launching the wrong Python — a version of Python on your computer that does not have the numpy packages installed.

You can test if this is the case by running the following on the command line and seeing what it outputs:

python -c "import sys; print(sys.executable)"

You can try the same from within RStudio:

system('python -c "import sys; print(sys.executable)"')

If it gives a different result, you can pass the result of the first as an absolute path to python (changing /path/to/python for the correct value for your system):

system('/path/to/python myfirstpythonfile.py')

As you mention in the comments that you are actually trying to use Python3, then you may be able to simply do the following from within RStudio:

system('python3 myfirstpythonfile.py')

This will run your script using your installed Python3 and the associated packages/libraries.

mfitzp
  • 15,275
  • 7
  • 50
  • 70
  • @Parfi did you try running the above commands and checking the output? – mfitzp May 19 '16 at 14:49
  • Maybe you are right. - python -c "import sys; print(sys.executable)" - gave me Python 2.7 - system('python -c "import sys; print(sys.executable)"') - gave me Python 2.7 but I use Python 3.5 (there I have all the libraries) But if I try system('Path/.../Programs/Python/Python35-32/ myfirstpythonfile.py'), I get warning massage with status 127 – Parfi May 19 '16 at 14:54
  • Ah use `python3 -c "import sys; print(sys.executable)"` to get the full path to your `python3` – mfitzp May 19 '16 at 14:56
  • @Parfi see the edit: you may be able to just call `python3` from inside RStudio – mfitzp May 19 '16 at 14:59
  • I had seen. Thank you, now 1. program work correct, but 2. program has new problem. Exactly this: http://stackoverflow.com/questions/34588339/import-scipy-sparse-failed And this problem don't like me, because I will have to install Python 3.4 and all libraries, etc. :-) – Parfi May 19 '16 at 16:24
  • @Parfi If this solution worked for you can accept the answer (the tick mark). I'll take a look at the other question – mfitzp May 20 '16 at 09:51