R Reticulate: ModuleNotFoundError: No module named 'pandas' - r

I am using the Azure ML Notebook with R kernel to use a python script through reticulate package but I am unable to install pandas library which is using in the python script. I am following the instructions from here. I have written the following R code:
library(reticulate)
Sys.which("python")
use_python("/usr/bin/python3.8")
When I use py_config() then it gives me the following output:
python: /usr/bin/python3.8
libpython: /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so
pythonhome: //usr://usr
version: 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
numpy: [NOT FOUND]
Now, conda_list() shows the r-reticulate environment and its location as /anaconda/envs/r-reticulate/bin/python and when I try to install pandas in default r-reticulate environment using:
conda_install("r-reticulate", "pandas")
Then it gives the following output:
+ '/anaconda/bin/conda' 'install' '--yes' '--name' 'r-reticulate' '-c' 'conda-forge' 'pandas'
But when I use pandas <- import("pandas") then it gives me the following error message:
Error in py_module_import(module, convert = convert): ModuleNotFoundError: No module named 'pandas'
Can anyone please tell me whether this is issue related to path or something else and how to fix it.
Any help would be appreciated.

Not sure what the problem is, but have you tried
reticulate::py_install('pandas', pip = TRUE)

Related

Error loading the keras package in R studio

I am using R4.0.1 and Rstudio1.3.959 on windows 10. I have installed tensor flow:
install.packages("tensorflow")
library(tensorflow)
install_tensorflow(method = "conda", conda_python_version = 3.6)
I checked the installation success by:
library(tensorflow)
tf$constant("Hellow Tensorflow")
Output: Tensor("Const:0", shape=(), dtype=string)
tf$constant(1.5)
Output: Tensor("Const_1:0", shape=(), dtype=float32)
I further checked by:
tf_config()
Output:
TensorFlow v1.13.2 ()
Python v3.6 (C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe)
From here it seems the tensor flow is installed properly and is working fine.
However, I faced problems in loading Keras library. I did the following:
install.packages("keras")
library(keras)
As I load the library, it gives the following error
Error: package or namespace load failed for ‘keras’:
.onLoad failed in loadNamespace() for 'keras', details:
call: py_module_import(module, convert = convert)
error: ImportError: cannot import name 'swish'
Detailed traceback:
File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\__init__.py", line 14, in <module>
from . import activations
File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\activations\__init__.py", line 23, in <module>
from tensorflow.python.keras.activations import swish
Since the keras package could not be loaded I cannot run the following code
install_keras(method = "conda")
I could get additional information as below:
library(reticulate)
> py_discover_config("keras")
python: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe
libpython: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python36.dll
pythonhome: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate
version: 3.6.10 |Anaconda, Inc.| (default, May 7 2020, 19:46:08) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy
numpy_version: 1.18.1
I could also see that default python version in use as:
Sys.which("python")
python
"C:\\Users\\user\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1\\python.exe"
I would be grateful if anyone can solve this installation issue. Thanks
I am a Windows 10 User and I encountered the same issue today. After checking multiple posts, the below steps worked for me.
Update R to the latest 4.1.2 version. R Studio might say that R is up to date (which happened to me today), but it may not be.
install.packages("installr")
library(installr)
updateR()
Update Rcpp, jsonlite, and curl packages even if they claim to be up to date.
install.packages("Rcpp")
install.packages("jsonlite")
install.packages("curl")
Start a Fresh R session.
Run the below commands to install, reticulate, tensorflow, and keras from github:
devtools::install_github("rstudio/reticulate")
devtools::install_github("rstudio/tensorflow")
devtools::install_github("rstudio/keras")
Run the below commands (Some of them may be redundant if preceding ones include the functionalities. Entering them here anyway as it could help.).
library(keras)
install_keras()
library(reticulate)
library(tensorflow)
Test TensorFlow installation with the command below:
tf$constant("Hello World!")
If you get the below output, you're there. There could be some text before the output (ignore them).
tf.Tensor(b'Hello World!', shape=(), dtype=string)
When running a DNN or RNN, after installing the packages, calling just library(keras) shoudl be adequate.
Have fun!
This might be a bit different but I find it easier to hand manage dependent env. So you can open conda prompt and execute:
conda create -n env_name python=3.6 tensorflow
Then in R before you do anything call
library(keras)
library(tensorflow)
use_condaenv(condaenv = "env_name",required = T)
By default you wil get TF 2.xx, you can specify that in conda env.
EDIT: For TF gpu you need to specify conda create -n env_name python=3.6 tensorflow-gpu and you will get CUDa and CUDNN if you have GPU on your PC.

Specify reticulate python path in RETICULATE_PYTHON environment variable

Whenever I use reticulate in RStudio, the default REPL is using python2.7, but I want to use python3 per default.
I have added the python path to python3 to my .bashrc in the environment variable RETICULATE_PYTHON and when I use R and reticulate from the command line, Sys.getenv('RETICUALTE_PYTHON') returns /usr/bin/python3. If open a REPL in the command line using, I get the correct path. If I do the same in RStudio, I get an empty string.
R
Sys.getenv('RETICULATE_PYTHON')
Return in R (from command line):
[1] "/usr/bin/python3"
in RStudio:
[1] ""
In the RStudio Terminal the output is correct:
echo $RETICULATE_PYTHON
/usr/bin/python3
Also, when I start R from the command line, py_config() is this:
> library(reticulate)
> py_config()
python: /usr/bin/python3
libpython: /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so
pythonhome: /usr:/usr
version: 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0]
numpy: /usr/lib/python3/dist-packages/numpy
numpy_version: 1.14.5
NOTE: Python version was forced by RETICULATE_PYTHON
But in RStudio it is this:
> library(reticulate)
> py_config()
python: /usr/bin/python
libpython: /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome: /usr:/usr
version: 2.7.15+ (default, Oct 2 2018, 22:12:08) [GCC 8.2.0]
numpy: /usr/lib/python2.7/dist-packages/numpy
numpy_version: 1.14.5
python versions found:
/usr/bin/python
/usr/bin/python3
Restarting RStudio did not help. Any suggestions on how to make RStudio use the correct python binary as well?
The solution I used was simply to set the default Python interpreter for RStudio (2021.09.1 for Windows) using Tools > Global Options to the path of the interpreter I wanted RStudio to use.
In my case, I set it too use "C:/tools/Anaconda3/envs/dev/python.exe".
I assume the same solution would work for Linux too.
When faced similar issue I solved it by specifying Python configuration before loading the reticulate package:
Sys.setenv(RETICULATE_PYTHON = "C:\\ProgramData\\Anaconda3")
library(reticulate)
The documentation says to set it in .Rprofile with
Sys.setenv(RETICULATE_PYTHON="/path/to/your/preferred/python")
if you always want a fixed Python. Otherwise, do as #nba2020 suggests to set it in a particular script.

R Reticulate: 'Unable to locate conda environment' when attempting to use_condaenv(required = TRUE)

I see variations of this question have been asked but none of the answers have resolved my problem. I'm using Windows 10, R version 3.6.2, RStudio version 1.2.5033, reticulate version 1.14
> library(reticulate)
> library(dplyr)
> conda_list()
name python
1 r-miniconda C:\\Users\\caleb\\AppData\\Local\\r-miniconda\\python.exe
2 r-reticulate C:\\Users\\caleb\\AppData\\Local\\r-miniconda\\envs\\r-reticulate\\python.exe
So I try to use the second item:
> conda_list()[[2]][2] %>%
+ use_condaenv(required = TRUE)
But I get the following error:
Error in use_condaenv(., required = TRUE) :
Unable to locate conda environment 'C:/Users/caleb/AppData/Local/r-miniconda/envs/r-reticulate/python.exe'.
Looking for available versions gets me:
> py_discover_config()
python: C:/Users/caleb/AppData/Local/r-miniconda/envs/r-reticulate/python.exe
libpython: C:/Users/caleb/AppData/Local/r-miniconda/envs/r-reticulate/python36.dll
pythonhome: C:/Users/caleb/AppData/Local/r-miniconda/envs/r-reticulate
version: 3.6.10 |Anaconda, Inc.| (default, Jan 7 2020, 15:18:16) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/caleb/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy
numpy_version: 1.18.1
This is my first attempt use Python from R so I'm confident this is a user-error issue but searching the error message and looking through related stack threads hasn't helped. Does anyone see what I'm doing wrong here?
The use_condaenv function expects an environment name, not the path to the python executable. So just use
use_condaenv("r-reticulate")
rather than trying to call use_condaenv("C:\\Users\\caleb\\AppData\\Local\\r-miniconda\\envs\\r-reticulate\\python.exe")

Can't figure out how to use conda environment after reticulate::use_condaenv(path)

I created a conda environment using the terminal:
conda create --name pathfinder_example_proj_env python=3.6 feather-format=0.4.0 statsmodels=0.9.0
I also created a trivial python script
import feather
import pandas as pd
import statsmodels.api as sm
print("Done")
In an R notebook, I now want to run that script from within the conda environment I created earlier.
I tried:
reticulate::use_condaenv("pathfinder_example_proj_env", required = TRUE)
reticulate::source_python("../python/python_model.py")
But I get the following error:
Error in py_run_file_impl(file, local, convert) : ImportError: No module named feather
When I check the version of python reticulate is using I get:
reticulate::py_config()
python: /usr/bin/python
libpython: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome: /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version: 2.7.10 (default, Oct 6 2017, 22:29:07) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
numpy: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy
numpy_version: 1.8.0
python versions found:
/usr/bin/python
/Users/bradcannell/anaconda/bin/python
/Users/bradcannell/.virtualenvs/bradcannell-_MDC9FPE/bin/python
I checked for available versions using py_discover_config()
reticulate::py_discover_config()
python: /usr/bin/python
libpython: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome: /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version: 2.7.10 (default, Oct 6 2017, 22:29:07) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
numpy: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy
numpy_version: 1.8.0
python versions found:
/usr/bin/python
/Users/bradcannell/anaconda/bin/python
/Users/bradcannell/.virtualenvs/bradcannell-_MDC9FPE/bin/python
/Users/bradcannell/anaconda/envs/pathfinder_example_proj_env/bin/python
And as you can see, the virtual environment is listed. I'm just not sure how to use it.
I've read all the articles on the reticulate website:
https://rstudio.github.io/reticulate/index.html
I also found a couple of threads on Github:
https://github.com/rstudio/reticulate/issues/1
https://github.com/rstudio/reticulate/issues/292
This works for me;
library(reticulate)
myenvs=conda_list()
envname=myenvs$name[2]
use_condaenv(envname, required = TRUE)
# or
use_condaenv("r-miniconda", required = TRUE)
restart r session is needed sometimes.
I found the solution here: https://community.rstudio.com/t/reticulate-source-python-and-exec-problems/7386/6
After installing the development version of reticulate (devtools::install_github("rstudio/reticulate") reticulate uses the conda environment as expected.
Leaving this post up in case anyone else runs into this issue.
This thing worked:
By setting the value of the RETICULATE_PYTHON environment variable to a Python binary. Note that if you set this environment variable, then the specified version of Python will always be used (i.e. this is prescriptive rather than advisory). To set the value of RETICULATE_PYTHON, insert Sys.setenv(RETICULATE_PYTHON = PATH) into your project’s .Rprofile, where PATH is your preferred Python binary.

R reticulate unable to find installed python library

I'm venturing into using Reticulate in R and having trouble installing a package, specifically psycopg2 but I've also tried installing twisted with the same result.
after I load reticulate in R I double check to make sure my package is installed:
> conda_install(envname = "r-reticulate", packages="psycopg2")
Solving environment: ...working... done
# All requested packages already installed.
Looks good. So I set my condaenv to r-reticulate just to be double sure. The docs say I should not have to do this, but it should not hurt:
> use_condaenv( "r-reticulate")
Then I try to import psycopg2:
> psycopg2 <- import('psycopg2')
Error in py_module_import(module, convert = convert) :
ImportError: No module named psycopg2
If I install twisted the same way, I get the same error:
> twisted <- import('twisted')
Error in py_module_import(module, convert = convert) :
ImportError: No module named twisted
So, what am I doing wrong here?
Below is the output of conda_list and py_config... I was expecting py_config to show me the path to the python exe in my r-reticulate environment. However I don't have anything to compare this to, so my expectations may have been wrong.
> conda_list()
name python
1 Anaconda C:\\Users\\jal\\AppData\\Local\\Continuum\\Anaconda\\python.exe
2 r-reticulate C:\\Users\\jal\\AppData\\Local\\Continuum\\Anaconda\\envs\\r-reticulate\\python.exe
> py_config()
python: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
libpython: C:/Users/jal/AppData/Local/CONTIN~1/Anaconda/python27.dll
pythonhome: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda
version: 2.7.15 |Anaconda custom (64-bit)| (default, May 1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\lib\site-packages\numpy
numpy_version: 1.14.3
pandas: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\lib\site-packages\pandas
python versions found:
C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\ANACON~1\python.exe
On a whim I downloaded and installed the latest RStudio desktop (1.2.701) and upgraded from 1.1.442. This seems to have solved my issue. The packages load and I now see that when I run py_config I see paths that reflect my environment choice, as I would expect.
> py_config()
python: C:\Users\jal\AppData\Local\Continuum\Anaconda\envs\r-reticulate\python.exe
libpython: C:/Users/jal/AppData/Local/Continuum/Anaconda/envs/r-reticulate/python36.dll
pythonhome: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1
version: 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1\lib\site-packages\numpy
numpy_version: 1.14.3
psycopg2: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1\lib\site-packages\psycopg2
python versions found:
C:\Users\jal\AppData\Local\Continuum\Anaconda\envs\r-reticulate\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\ANACON~1\python.exe
I solved my issue by following this way in RStudio:
py_config()
No non-system installation of Python could be found.
Would you like to download and install Miniconda?
Miniconda is an open source environment management system for Python.
See https://docs.conda.io/en/latest/miniconda.html for more details.
Would you like to install Miniconda? [Y/n]: Y
Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...
trying URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'
Content type 'application/octet-stream' length 57256056 bytes (54.6 MB)
downloaded 54.6 MB
Installing Miniconda -- please wait a moment ...
I solved my issue by following this way:
Close RStudio.
Open Terminal and activate your desired environment.
Run RStudio by $ open -na Rstudio
Now you have your anac

Resources