16

I am trying to use the reticulate package in a Rmd file. I first created a setup chunk as follows:

library(reticulate)
use_virtualenv("r-reticulate")
use_python("C:\\Python27")

Then I import pandas:

#importing libraries
import pandas 
 ImportError: No module named pandas

Detailed traceback: 
  File "<string>", line 1, in <module>  

I have checked that pandas is already installed from the python command line. Why am I getting an import error here?

umair durrani
  • 5,597
  • 8
  • 45
  • 85
  • 3
    Silly question, but did you install `pandas` while the virtualenv was active? – CodeSpent Jan 18 '19 at 22:25
  • @CodeSpent thanks for your comment. I guess I updated it to make sure that it was installed. Was that the culprit? What should I do now? – umair durrani Jan 18 '19 at 23:35
  • 2
    Well your virtualenv is isolating your application, so just `pip install pandas` from inside your virtualenv. – CodeSpent Jan 18 '19 at 23:39
  • 1
    Where do I do `pip install pandas`? Python does not recognize that. I can only do that in cmd on Windows and I am not sure if there is a cmd engine in knitr. Sorry, I am an R user and still learning Python. – umair durrani Jan 18 '19 at 23:44
  • 1
    Added an answer, let me know if that works or if you need some more clarification. :) – CodeSpent Jan 18 '19 at 23:53

3 Answers3

21

install the package in R using the py_install()

library(reticulate)
py_install("pandas")

refer this -> https://rstudio.github.io/reticulate/articles/python_packages.html

Nandan
  • 211
  • 2
  • 5
2

Solved. For /usr/bin/python3 pandas was available, but not for /usr/local/bin/python3

In RStudio, Tools/Global options... Halfway down is Python. Set the interpreter to /usr/bin/python3 After restarting R, import pandas now works.

Roger
  • 422
  • 4
  • 12
1

It appears pandas is not installed in your virtualenv. It may be on your machine, but your virtualenv isolates your application from the rest of your machine.

While your virtualenv is active:

  • Open cmd/bash
  • Run pip install pandas

Now pandas should be available to you within this env. Later you can generate a requirements.txt file that makes dependency management much easier.

CodeSpent
  • 1,684
  • 4
  • 23
  • 46
  • 2
    Thanks for your help, however, this didn't work for me. I first did `library(reticulate) use_virtualenv("r-reticulate") use_python("C:\\Python27")`. Then `pip install pandas` in cmd showed that requirement is already satisfied. Then I ran the `python` chunk `import pandas`. However, I still get `ImportError: No module named pandas`. I have also tried closing and re-opening rstudio but to no avail. – umair durrani Jan 19 '19 at 00:00
  • 1
    What about trying `py_install("pandas")`? – CodeSpent Jan 19 '19 at 00:07
  • `NameError: name 'py_install' is not defined` – umair durrani Jan 19 '19 at 00:10
  • 1
    Hmm. According to [this article](https://cran.r-project.org/web/packages/reticulate/vignettes/python_packages.html) that's the proper way of installing python packages. – CodeSpent Jan 19 '19 at 00:12
  • 1
    I've tried all the options and somehow managed to fail everytime! Gonna watch youtube videos on reticulate now. Thanks for your help @CodeSpent – umair durrani Jan 19 '19 at 01:23
  • Good luck, I'm sorry I don't really use RStudio so can't be too much help. Is RStudio a necessary prerequisite? It sounds like its going to make things a bit more complicated. – CodeSpent Jan 19 '19 at 01:29
  • Thanks for all your help! Yes, the preview version of RStudio is a requirement to use reticulate. However, I've abandoned it for now and I'm using the plain `python` engine in `rmarkdown`. That's working for me. – umair durrani Jan 19 '19 at 14:51
  • @umairdurrani did the ```python``` engine in ```rmarkdown``` worked for you? I am trying to install package ```ortools``` and it is throwing me some error. – Shibaprasadb Sep 22 '21 at 06:46
  • @Shibaprasadb Yes. Both `reticulate` and python engine in `rmarkdown` work for me. – umair durrani Sep 22 '21 at 11:34