1

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 Answer 1

0

I solved it with

Importing Python package (pandas) from R with Reticulate

reticulate::py_module_available("pandas") ## Returned FALSE
reticulate::py_install("pandas")
py_run_file('hello_world.py')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.