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.
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
.
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")
}
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:
conda create --name cloned_env --clone original_env
so it would look like this conda create --name reticulate --clone base
conda activate reticulate
to activate the environment 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.