Cannot get `reticulate` working in MacOS arm64 architecture

Question:

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.

Asked By: Harshvardhan

||

Answers:

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
Answered By: Harshvardhan

I had the same issue on my M1 MAcbook Air. I am also using anaconda but the problem was that the architecture of some dependency (libpython3.9.dylib in my case) was x86_64, I ran lipo -info /opt/anaconda3/lib/libpython3.9.dylib and the output was

Non-fat file: /opt/anaconda3/lib/libpython3.9.dylib is architecture: x86_64

Then I tried creating a native environment on anaconda following the tips from this question. I had only two environments (check with conda env list) so I ran

CONDA_SUBDIR=osx-arm64 conda create -n native numpy -c conda-forge

then checked that the environment had been created, then

conda activate native
conda config --env --set subdir osx-arm64

Then I ran again which python which gave /opt/anaconda3/envs/native/bin/python, and I put this last thing in the reticulate argument:

use_python("/opt/anaconda3/envs/native/bin/python", required = TRUE)

What was kind of pointless is that now trying to call modules from RSTudio requires me to install all the ones Anaconda already has but I cannot access to (at least not with reticulate).

Answered By: bob-ortiz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.