Cannot load python library with R2024a: API functions not available

41 次查看(过去 30 天)
I updated MATLAB to R2024a a few days ago, and I'm now trying the basic example to call Python from MATLAB here: https://ch.mathworks.com/help/matlab/call-python-libraries.html
I tried the following:
>> py.list
and see this error immediately:
Python API functions are not available.
I have the PythonEnvironment setup as follows:
>> pyversion('/Users/.../opt/anaconda3/bin/python')
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/.../opt/anaconda3/bin/python"
Library: "/Users/.../opt/anaconda3/lib/libpython3.9.dylib"
Home: "/Users/.../opt/anaconda3"
Status: NotLoaded
ExecutionMode: OutOfProcess
It appears I cannot get the status to turn to "Loaded", which used to happen earlier with R2023b. I am also using the apple silicon native version (maca64) of MATLAB, which could also be a factor here. I read https://ch.mathworks.com/matlabcentral/answers/1977529-how-to-use-python-from-matlab-on-mac-with-apple-silicon and have installed Amazon Corretto 11 from here: https://ch.mathworks.com/support/requirements/apple-silicon.html
Any idea how I could get the Python API to load? Many thanks in advance for your help!
  1 个评论
Amith Kamath
Amith Kamath 2024-5-30
Quick update: I uninstalled the maca64 version and installed the maci64 version (Intel Processors), and it appears I can now run Python code now. For more details about my hardware - the OS version is 14.5 (23F79) and the chip is Apple M1 (2020).

请先登录,再进行评论。

回答(1 个)

Shubham
Shubham 2024-6-17
Hi Amith,
Integrating Python with MATLAB on Apple Silicon requires careful attention to the compatibility between the MATLAB build (maci64 vs. maca64) and the Python build you're using. Here's a concise guide to help ensure that you set up your environment correctly for both scenarios:
1) Using maci64 MATLAB on Apple Silicon through Rosetta 2
  • Rosetta 2 Translation: If you're running a maci64 version of MATLAB (R2020b and later) on an Apple Silicon Mac, it operates under the Rosetta 2 translation layer. This setup allows you to run software built for Intel x86_64 architecture on Apple's M1 or M2 chips.
  • Python Environment: For this configuration, your Python installation needs to be a universal build or specifically an x86_64 (maci64) build to ensure compatibility. Universal builds of Python include both arm64 (for Apple Silicon) and x86_64 (for Intel) architectures.
  • Installing Python Packages: To install Python packages compatible with the maci64 architecture, use the following terminal command, which explicitly targets the x86_64 architecture:
arch -x86_64 python3 -m pip install myPythonPackage
Replace myPythonPackage with the name of the Python package you wish to install.
2) Using maca64 MATLAB (Native Apple Silicon Build)
  • Native Performance: Starting with MATLAB R2022b (Beta) and fully supported in R2023b and later, the maca64 build is optimized for Apple Silicon, providing better performance by leveraging the architecture directly.
  • Java Requirement: For the maca64 version of MATLAB, it's important to have the correct version of Java installed. As of the latest guidance, an “aarch64” version of Java 8 is recommended. This is because the x64 versions of Oracle Java 8 are not compatible with the native maca64 build of MATLAB.
  • Python Libraries: Your Python installation should be a universal build or specifically built for arm64 (maca64) to ensure compatibility with the native Apple Silicon version of MATLAB. Most modern Python distributions provide universal builds that work seamlessly across different architectures.
General Tips:
  • Check Python Version: Before integrating Python with MATLAB, verify that the Python version you're using is supported by your MATLAB release. MATLAB documentation provides detailed information on compatible Python versions.
  • Python Environment Verification: Use the pyenv command in MATLAB to verify the Python environment settings. Ensure that the Executable, Library, and Home paths are correctly set, and the Status should indicate that the Python environment is properly loaded. Refer to this documentation: https://in.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html
  • Troubleshooting: If you encounter issues, consider creating a virtual environment for Python. This can help manage dependencies more effectively and isolate your Python setup from system-wide installations.
By ensuring that your MATLAB and Python builds match in terms of architecture and paying close attention to the Java and Python versions required, you can achieve a seamless integration of Python functionalities within MATLAB, even on Apple Silicon Macs.
I hope this helps!

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by