11

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
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • 3
    I had a similar issue when using reticulate for the first time. Remove and recreated the `r-reticulate` conda environment from the shell solved my problem, see https://github.com/rstudio/reticulate/issues/253. – Raniere Silva Jun 29 '18 at 19:50
  • I have been struggling with this as well (on OS X) but none of these solutions worked. I installed RStudio 1.2.x, I added RETICULATE_PYTHON=/python3 to my .Renviron file, I removed and reinstalled conda env r-reticulate. I ran `conda_install('r-reticulate', 'psycopg2')` and same for 'numpy' but neither package shows up when I run `py_config()`. `import('psycopg2')` and `import('numpy')` both result in "ModuleNotFoundError: no module named ..." – Brian D Dec 13 '18 at 18:41
  • 1
    conda_install(...) installs every time I run it. It doesn't recognize that packages are already installed. – Brian D Dec 13 '18 at 18:46
  • In my case I had a ~/.virtualenvs/r-tensorflow folder from trying to install tensorflow. Removal of that directory fixed the issue. – wordsforthewise Mar 19 '19 at 00:47
  • Restarting RStudio solved this issue for me. – mikoontz Oct 23 '19 at 18:13

3 Answers3

7

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
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • I could see the python versions by `py_config` but still no success. Just running in the correct environment from Terminal does the work. – Shayan Amani Sep 10 '19 at 21:02
1

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 ...

  • 1
    that's fine if you want you tu use the **conda environment**. in my case. for those using another virtual environment, as **pipenv** for instance, it does not work.. – rodolfoksveiga Mar 23 '21 at 14:50
0

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

Shayan Amani
  • 5,787
  • 1
  • 39
  • 40