pythonrmacosrstudiopyenv

RStudio discovers Python pyenv installation, but I'm unable to run Python processes


I'm trying to run Python in RStudio. I have installed Python 3.8.5 via pyenv, and created a virtual environment with pyenv virtualenv (which I call venv-Rstudio) in my RStudio project folder. Based on the instructions given here, I think I have correctly managed to indicate to RStudio which Python environment to use:

>Sys.setenv(RETICULATE_PYTHON="/Users/my_username/.pyenv/versions/venv-Rstudio/bin/python")
> py_discover_config()
python:         /Users/my_username/.pyenv/versions/3.8.5/envs/venv-Rstudio/bin/python
libpython:      [NOT FOUND]
pythonhome:     /Users/my_username/.pyenv/versions/3.8.5:/Users/my_username/.pyenv/versions/3.8.5
version:        3.8.5 (default, Oct  1 2020, 21:50:37)  [Clang 12.0.0 (clang-1200.0.32.2)]
numpy:          /Users/my_username/.pyenv/versions/3.8.5/envs/venv-Rstudio/lib/python3.8/site-packages/numpy
numpy_version:  1.18.5

When trying to call Python, I get the following error:

> py_config()
Error: Python shared library not found, Python bindings not loaded.
Use reticulate::install_miniconda() if you'd like to install a Miniconda Python environment.

I suspect this has something to do with libpython: [NOT FOUND] seen above, but I have made sure libpython is installed in the venv-Rstudio virtual environment:

(venv-Rstudio) my_username@MBP15 Rstudio % pip list
Package                Version
---------------------- ---------
...
Keras                  2.4.3
Keras-Preprocessing    1.1.2
libpython              0.2
Markdown               3.2.2
multidict              4.7.6
numpy                  1.18.5
...

Any ideas what I might be missing and how to fix this? I'm running macOS 10.15.7 Catalina, RStudio 1.3 with R version 4.0.2.


Solution

  • After posting my question, this question appeared in the Related list, and this answer by @nucholab solved my problem. In short, re-installing Python with pyenv using the following command enables shareable libraries:

    env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.8.5 
    

    After that and re-creating my virtual environment (and for the record, installing numpy), loading Python in RStudio works as expected:

    > py_config()
    python:         /Users/my_username/.pyenv/versions/3.8.5/envs/venv-Rstudio/bin/python
    libpython:      /Users/my_username/.pyenv/versions/3.8.5/lib/libpython3.8.dylib
    pythonhome:     /Users/my_username/.pyenv/versions/3.8.5:/Users/andreas/.pyenv/versions/3.8.5
    version:        3.8.5 (default, Oct  2 2020, 13:54:11)  [Clang 12.0.0 (clang-1200.0.32.2)]
    numpy:          /Users/my_username/.pyenv/versions/3.8.5/envs/venv-Rstudio/lib/python3.8/site-packages/numpy
    numpy_version:  1.19.2
    tensorflow:     [NOT FOUND]
    
    NOTE: Python version was forced by RETICULATE_PYTHON