1

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?

Vitomir
  • 295
  • 2
  • 14
  • 1
    Could you try to reduce the `.py` script to only `import pandas` and source it to see if the error already occurs at this stage, and could help to find a solution. – Waldi Jun 09 '21 at 16:54
  • Tyr to specify which Python version you are using. Make sure that the python environment you use via `reticulate` has `pandas` too. You can use `use_python("PATH TO YOUR PYTHON ")` OR, you can import `pandas` by using the default `reticulate` python version. To install `pandas` to the current python environment, you can use `py_install("pandas")` – mustafaakben Jun 10 '21 at 15:47
  • @Waldi: an empty script with only "import pandas as pd" produces the same error. – Vitomir Jun 14 '21 at 08:22
  • @Mustafaakben: I have tried installing again with py_install or using "reticulate::" but still same issue – Vitomir Jun 14 '21 at 08:22

1 Answers1

1

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.

eduardokapp
  • 1,612
  • 1
  • 6
  • 28