Unable to resolve the name 'py.anything' when using Homebrew Python

I'm on an Apple Silicon mac, and the default python3 that Matlab finds (in /Library/Developer/CommandLineTools/usr/bin/python3, found with the setting pyenv('Version', 'python3')) is problematic because its pip can't seem to install numpy. The version that comes with Homebrew can build everything just fine, so I tried
>> pyenv('Version','/opt/homebrew/bin/python3')
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/opt/homebrew/opt/python@3.9/bin/python3.9"
Library: "/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9"
Status: NotLoaded
ExecutionMode: InProcess
but now, I can't access anything through py: it just says "Unable to resolve the name", making it unusable.
>> py.None
Unable to resolve the name 'py.None'.
Any ideas on why this is, or how to work around it? Perhaps something like the homebrew Python is ARM-native code (I don't know if that's the case though), and there's some kind of linking issue because Matlab is on Rosetta?

4 Comments

You may have already seen @Jorian Khan's solution to a similar problem, https://www.mathworks.com/matlabcentral/answers/1764800-pyenv-library-property-empty-unable-to-resolve-the-name-linux-pop-os-22-04-python-3-9-r2022a?s_tid=srchtitle#answer_1013335 , where Jorian modifies the settings.json entry to explicitly refer to paths to the desired Python installation. The same may work for you to point to your Homebrew installation of Python.
@Al Danial Thanks for the suggestion. That does look like a related issue. Unfortunately, that answer is on Ubuntu, and I couldn't find a settings.json file belonging to Matlab anywhere on a macOS system (I looked inside /Application, ~/Library, and also tried variations like looking for a directory named python instead). Do you have any idea where I can find a comparable setting on the mac?
In case anyone else runs in to the same issue: I contacted Tech Support over this and it seems this is a Rosetta - ARM native linking problem. Apparently the only solution is to either compile x86-64 numpy over Rosetta (couldn't figure out how) or to wait for an ARM native Matlab to appear...
I have exactly the same issue with macports' python3.9. However, the Matlab-python binding works indeed with the "native" (command line tools) macos python3 (3.9).
What I don't understand is that if it's related with the ARM native linking, the native macOS python shouldn't work neither right?
(Waiting for the R2022b ARM compatible beta to come out, let's see how it goes...)

Sign in to comment.

Answers (2)

I won't have a macOS machine handy for a while so I can't experiment. I imagine the MathWorks tech support would know... unless this level of fiddling falls outside the realm of what users ought to be doing.
I ran into this same issue this morning. After a lot of trial and error, here is a solution that worked for me:
First, you need to have a second version of homebrew for installing intel-based apps: https://medium.com/mkdir-awesome/how-to-install-x86-64-homebrew-packages-on-apple-m1-macbook-54ba295230f
If you follow those instructions, you'll get to a point where you have added these lines to your .zsh profile:
export PATH=$HOME/bin:/usr/local/bin:$PATH# for intel x86_64 brew
alias axbrew='arch -x86_64 /usr/local/homebrew/bin/brew'
You can then use axbrew to install the x86_64 version of python3.9:
axbrew install python@3.9
After installation, i ran
axbrew unlink python@3.9
to be sure this version wasn't going to be accidently called by other programs
You should now have x86_64 versions of pip3.9 and python3.9 executables in: /usr/local/homebrew/Cellar/python@3.9/3.9.15/bin
You can use that pip3.9 to install numpy and other packages.
Then in matlab:
pyenv('Version','/usr/local/homebrew/Cellar/python@3.9/3.9.15/bin/python3.9');

Products

Release

R2022a

Tags

Asked:

on 22 Jul 2022

Commented:

on 30 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!