Import Errors with Python script run in R
Question: 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?


Answer: 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.

