5

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.

The Lyrist
  • 434
  • 3
  • 13

1 Answers1

2

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.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    Thanks @serv-inc. Yes, the python3 installation has NumPy installed. Most solutions out there seem to refer to /usr/bin/python but for mac it would be /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 etc, which caused me to second guess my path obtained by `which python3`. Thanks again for your help and pointing me to the document that indicates the need for a clean R session. – The Lyrist Sep 23 '18 at 05:41
  • @TheLyrist: you seemed a smart enough fellow to figure out the part with the path. Good that it's working. – serv-inc Sep 23 '18 at 05:45