Search code examples
pythonrreticulate

Cannot get `reticulate` working in MacOS arm64 architecture


I have been trying to use Python in RStudio but apparently, the versions do not match. I have the reticulate version 1.23 installed. I get the following error.

> reticulate::repl_python()
Error in py_initialize(config$python, config$libpython, config$pythonhome,  : 
  /Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib - dlopen(/Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib, 0x000A): tried: '/Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libpython3.8.dylib' (no such file)

However, I have Python installed — I can use it using Anaconda. I can also verify it using Sys.which("python").

> Sys.which("python")
           python 
"/usr/bin/python" 

Another check.

> reticulate::conda_list()
          name                                                         python
1  r-miniconda             /Users/harshvardhan/Library/r-miniconda/bin/python
2         base                   /Users/harshvardhan/opt/anaconda3/bin/python
3 r-reticulate /Users/harshvardhan/opt/anaconda3/envs/r-reticulate/bin/python

Whenever I run any Python code from my .py script, I get the following error.

> reticulate::repl_python()
Error in py_initialize(config$python, config$libpython, config$pythonhome,  : 
  /Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib - dlopen(/Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib, 0x000A): tried: '/Users/harshvardhan/opt/anaconda3/lib/libpython3.8.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libpython3.8.dylib' (no such file)
> import numpy as np
Error: unexpected symbol in "import numpy"

Here's the system information. Any help would be great.

> Sys.info()
                                                                                               sysname 
                                                                                              "Darwin" 
                                                                                               release 
                                                                                              "21.2.0" 
                                                                                               version 
"Darwin Kernel Version 21.2.0: Sun Nov 28 20:29:10 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T8101" 
                                                                                              nodename 
                                                                     "Harshvardhans-MacBook-Air.local" 
                                                                                               machine 
                                                                                               "arm64" 
                                                                                                 login 
                                                                                                "root" 
                                                                                                  user 
                                                                                        "harshvardhan" 
                                                                                        effective_user 
                                                                                        "harshvardhan" 

Some resources I've tried to no avail. I'd be ecstatic to learn if I missed something.


Solution

  • I think I figured it out. Here's what worked for me.

    First, I opened Terminal and found my Python 3 location.

    which python
    

    gave me

    /Users/harshvardhan/opt/anaconda3/bin/python
    

    Then, I loaded reticulate.

    library(reticulate)
    

    Then I ran use_python() with required = T as suggested here. Then it worked as expected!

    library("reticulate")
    use_python("/usr/bin/python", required = T)
    sys = import("sys")
    >>> for i in range(10):
    ...    print(i)
    ... 
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9