run python script within R

Viewed 72

I have 'hello_world.py`:

import pandas as pd

print('hello world')
df=pd.DataFrame({'col':[1,2,3]})
df
df.to_csv('hello.csv')

I would like to call this script from and R script.

Using shell from Run python script from R did not work --> running shell('"python hello_world.py"') gave me the following error:

    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

Using system('python hello_world.py') returned the same error, as suggested by how to call python script from R with arguments

Using reticulate as per Calling Python from R with reticulate package and

library(reticulate)
py_run_file('hello_world.py')

returned:

Error in py_run_file_impl(file, local, convert) : 
  ModuleNotFoundError: No module named 'pandas'

Including the extra line !pip install pandas produced a syntax error:

Error in py_run_file_impl(file, local, convert) : 
    File "<string>", line 1
    !pip install pandas
    ^
SyntaxError: invalid syntax

Is there something I am missing? Any suggestions welcome!

1 Answers
Related