reticulate ImportError: No module named pandas in Rstudio version 1.2
Asked Answered
C

3

17

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?

Chanel answered 18/1, 2019 at 21:52 Comment(5)
Silly question, but did you install pandas while the virtualenv was active?Interchange
@Interchange 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?Chanel
Well your virtualenv is isolating your application, so just pip install pandas from inside your virtualenv.Interchange
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.Chanel
Added an answer, let me know if that works or if you need some more clarification. :)Interchange
B
22

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

Butts answered 20/4, 2020 at 18:48 Comment(0)
D
3

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.

Demimondaine answered 30/12, 2022 at 18:23 Comment(1)
I had the same problem and this did it. Had to point it to my local installation of Python.Sea
I
2

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.

Interchange answered 18/1, 2019 at 23:52 Comment(10)
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.Chanel
What about trying py_install("pandas")?Interchange
NameError: name 'py_install' is not definedChanel
Hmm. According to this article that's the proper way of installing python packages.Interchange
I've tried all the options and somehow managed to fail everytime! Gonna watch youtube videos on reticulate now. Thanks for your help @InterchangeChanel
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.Interchange
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.Chanel
@umairdurrani did the python engine in rmarkdown worked for you? I am trying to install package ortools and it is throwing me some error.Peracid
@Shibaprasadb Yes. Both reticulate and python engine in rmarkdown work for me.Chanel
Super strange, this worked for me although it was 100% installed in the "r-reticulate" virtual environment before. I dont know why it works now.. but thank you!Nidianidicolous

© 2022 - 2024 — McMap. All rights reserved.