2

I have been learning python for about a year now. I just downloaded R to try using Python with Reticulate. I have all of my python packages pip installed in an anaconda base environment named base. I am getting everything to work except pandas. It’s the most bizzare thing.

Here’s my code:

{r}
library(reticulate)
use_condaenv(“base”)
{python}
import numpy as np
import matplotlib.pyplot as plt

This works fine and I’m able to use both packages in RStudio. However, when I try to import pandas as pd, I get the error

ImportError: No Module named pandas...

I most definitely have pandas pip installed in this base environment along with the other two packages. Just to be sure, I double checked by running a pip install for all 3 again. Why in the world won’t pandas work? It’s so frustrating! FWIW: seaborn (and other packages) doesn’t work either and is installed as well.

bismo
  • 1,257
  • 1
  • 16
  • 36
  • Maybe You have another installation of Python? Are You using R installed from Anaconda? – ipj Aug 24 '20 at 22:13
  • I am not using R installed from Anaconda. Could that be the issue? – bismo Aug 24 '20 at 22:15
  • Did You try import python module using `import`? For example `library(reticulate) os <- import("os") os$listdir(".")` As in @Eiri answer, multiple pythons may be the problem. – ipj Aug 24 '20 at 22:17
  • if you installed conda, I would strongly recommend you install packages with that (e.g., `conda install pandas`) – Paul H Aug 24 '20 at 22:21
  • @PaulH Thanks for the response. Virtual environments seem really, really confusing. What is the difference between conda and pip? The tutorial I used when I first downloaded python suggested to pip install all packages. – bismo Aug 24 '20 at 22:25
  • pip stands for "pip install (python) packages" that's it. that's all it does. conda is a full-fledged package and virtual environment manager that can install anything from postres to R to python packages. – Paul H Aug 24 '20 at 22:28
  • @bismo here's summary i wrote for a friend who was coming to python/conda from an R background: https://gist.github.com/phobson/8a4808bf6879f5f2cfb4 – Paul H Aug 24 '20 at 22:30

3 Answers3

0

Maybe you have installed multiple versions of Python and you're running a different one if it shows up when you run pip freeze. I suggest installing virtualenv inside your project folder to avoid conflict with other packages.

pip install virtualenv
virtualenv venv
source venv/bin/activate

And then install pandas inside.

pip install pandas

You can also try installing pandas with pip3.

Eiri
  • 3
  • 3
  • How do I find out if I have multiple pythons installed? – bismo Aug 24 '20 at 22:21
  • @bismo run `ls -l /usr/bin/python*` – Eiri Aug 24 '20 at 22:31
  • I ran it but I have no idea what any of this means. It returned multiple lines, 3 of which end in ```/Python.framework/Versions/2.7/bin/python2.7```,```/Python.framework/Versions/2.7/bin/python2.7-config``` and ```Python.framework/Versions/2.7/bin/pythonw2.7```. Does this mean I have (at least) 3 different pythons? How does that even happen? – bismo Aug 24 '20 at 22:33
  • @bismo No. A bunch of them are symlinks and config files. Did you get other versions too? And run `pip -V` to check which Python version `pip` is connected with. It has to be the same version that you are running. – Eiri Aug 24 '20 at 22:43
  • Those were the only 3 versions I got. When I ran ```pip -V``` it returned python 3.7, which is what I downloaded initially. – bismo Aug 24 '20 at 22:46
  • Well you're probably running with the 2.7 version. Issues like this happen a lot when the environment is messy. You can install Anaconda and run `conda create -n envname python=3.6` (Or any version or envname that you want.) And activate it with `conda activate envname`. The just install using `pip`. Your command line like should say `(envname)`. Run `conda deactivate` to deactivate the environment you're in. – Eiri Aug 24 '20 at 22:50
0

It seems as if you are using a Mac based on the output with the 'Frameworks'. Python 2.7 is the base version on the Mac. Best thing is to install Anaconda3 which will create a separate new base environment.

For reticulate, you need PyQt5 to render Python with R Markdown. So, do the following steps to set it up:

  1. Download and install Anaconda
  2. conda create --name cloned_env --clone original_env so it would look like this conda create --name reticulate --clone base
  3. conda activate reticulate to activate the environment
  4. pip install PyQT5 into the reticulate environment

Now check your Python now:

which python3

This will give you a path with Anaconda3 in the path. In a standard (Documents) directory create a text file called .Renviron.txt

Add the following code:

RETICULATE_PYTHON="your path from which python3"

For example, mine is the following, yours should be an anaconda one:

RETICULATE_PYTHON="/anaconda3/bin/python" 

it could also be

RETICULATE_PYTHON="/anaconda3/envs/reticulate"

Then when you fire up RStudio, you should have everything you need.

It is pretty well documented that this command does not work:

use_condaenv(“base”)

In R, check the command Sys.getenv() it should confirm that your RETICULATE_PYTHON variable is set to you specified path in the .Renviron file.

There is more discussion about this at this link:

Unable to change Python path in reticulate

Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
-1

You can do look into this source:

Hope it helps: R Interface to Python

Import a Python module

Source: R/python.R

Import the specified Python module for calling from R.

import(module, as = NULL, convert = TRUE, delay_load = FALSE) 
import_main(convert = TRUE)
import_builtins(convert = TRUE)
import_from_path(module, path = ".", convert = TRUE)

Arguments

module

Module name

as

Alias for module name (affects names of R classes). Note that this is an advanced parameter that should generally only be used in package development (since it affects the S3 name of the imported class and can therefore interfere with S3 method dispatching).

convert

TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function.

delay_load

TRUE to delay loading the module until it is first used. FALSE to load the module immediately. If a function is provided then it will be called once the module is loaded. If a list containing on_load() and on_error(e) elements is provided then on_load() will be called on successful load and on_error(e) if an error occurs.

path

Path to import from

Value

A Python module

Details

The import_from_path function imports a Python module from an arbitrary filesystem path (the directory of the specified python script is automatically added to the sys.path).

Examples

if (FALSE) 
{ 
    main <- import_main() 
    sys <- import("sys") 
 }
  • link-only answers aren't appropriate for this website. please summary the relevant info found there. – Paul H Aug 24 '20 at 22:29