The Lyrist
The Lyrist

Reputation: 444

How to properly set the Python path for the reticulate package in OSX High Sierra?

I have installed Python 3.7 in the default location for High Sierra using the official OSX package downloaded from the official Python site. When I run

which python3

I get the path

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

I then run the following lines in R Studio

reticulate::use_python(python = '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3')
sys <- import("sys")
sys$version

It seems that I am still pointing to the default installation of 2.7

[1] "2.7.10 (default, Oct  6 2017, 22:29:07) \n[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]"

I tried many other paths such as

/usr/local/bin/python3
/usr/local/bin
/Library/Frameworks/Python.framework/Versions/3.7/lib
/usr/bin/python
/Applications/Python 3.7

etc., but none seems to work. (it still shows 2.7.10)

Obviously, I have tried googling for the solution but to no avail unfortunately. Any guidance would be greatly appreciated.


Update: I finally got it to work by:

  1. Restarting the R Session as recommended by serv-inc
  2. Running the following commands:

    library(reticulate) reticulate::use_python(python = '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3', required = T) sys <- import("sys") sys$version

    to get the following response:

    [1] "3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) \n[Clang 6.0 (clang-600.0.57)]"

  3. If I have incorrectly specified an incorrect path such as /usr/bin/python, I would need to restart the R session or else reticulate would continue referring to the existing Python version.

In short, the problem was caused by the incorrect path specified in the initial call to the reticulate::use_python function, and subsequent calls with the correct path would not take effect as it requires a 'fresh' R session.

Upvotes: 5

Views: 6782

Answers (1)

serv-inc
serv-inc

Reputation: 38247

See https://github.com/rstudio/reticulate/issues/45:

Do

library("reticulate")
use_python("/usr/bin/python", required = T)

Before anything else.

See also https://github.com/rstudio/reticulate/issues/227:

reticulate will always prefer a version of Python that includes NumPy to one that doesn't. Does the version at /usr/local/bin/python3 have NumPy?

Obviously, I have tried googling for the solution

Sometimes, googling only the function name "reticulate::use_python" helps.

Upvotes: 2

Related Questions