pythonmatlabcondaapple-m1mini-forge

Matlab python integration on Apple Silicon


I installed Python on my Apple M1 using miniforge

brew install --cask miniforge   

but I am not able to use a Conda environment on MATLAB:

>> pyenv('Version', '/opt/homebrew/Caskroom/miniforge/base/envs/matpy39/bin/python3')

ans = 

  PythonEnvironment with properties:

          Version: "3.9"
       Executable: "/opt/homebrew/Caskroom/miniforge/base/envs/matpy39/bin/python3"
          Library: "/opt/homebrew/Caskroom/miniforge/base/envs/matpy39/lib/libpython3.9.dylib"
             Home: "/opt/homebrew/Caskroom/miniforge/base/envs/matpy39"
           Status: NotLoaded
    ExecutionMode: InProcess

>> py.list({1})
Unable to resolve the name py.list.

However, if I use Macs built in python version, it does work:

>> pyversion('/usr/bin/python3')
>> py.list({1})

ans = 

  Python list with no properties.

    [1.0]

I am not sure what to do so that MATLAB uses a specific Conda environment


Solution

  • Homebrew builds native ARM/M1 binaries on Apple Silicon now. Matlab is an x64 binary running under Rosetta emulation. I suspect the problem is that you can't load an ARM library (Homebrewed Python) into an x64/Rosetta process (Matlab). I bet macOS's system Python distribution is a "universal" build or similar thing that can load in to either kind of process.

    You can probably get this to work by installing an x64 version of Miniforge or Anaconda outside Homebrew, and telling Matlab to load that.

    Have a look at https://www.anaconda.com/blog/apple-silicon-transition.