Search code examples
rpandasreticulate

Error while importing pandas in R via reticulate


I am using R and I want to use a function I wrote in Python which needs to import pandas. Hence, I use the following code in R:

library(reticulate)
reticulate::py_install("pandas", force = TRUE)

which runs with no issues. Also, I already installed pandas in Python. Nevertheless, when I run the script which imports pandas:

source("script_with_pandas.py")

I get the following error:

Error in py_run_file_impl(file, local, convert) : 
  ImportError: C extension: No module named 'pandas._libs.interval' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --force' to build the C extensions first.

Any idea how to solve this?


Solution

  • Try

    reticulate::source_python("script_with_pandas.py")
    

    But I'm pretty sure this is an environment issue. If you're using RStudio >=v1.4 then you can go to tools --> global options --> python interpreter and check which one you're using, that may be the problem. Aside from that, I think it's only a matter of installing the pandas package onto the right environment.