解決 Mac M1 安裝 Tensorflow 後,遇到 Library not loaded: @rpath/libcblas.3.dylib

根據 Apple 文件「Tensorflow Plugin - Metal - Apple Developer」安裝 Tensorflow,但是執行 Tensorflow 卻遇到錯誤訊息。解決方式是重新安裝或者是更新 NumPy 版本。

programming
Photo by Startup Stock Photos on StockSnap


錯誤狀況

在終端機輸入指令,進到 Python 虛擬環境

source ~/miniforge3/bin/activate 

輸入指令 

python

import tensorflow as tf

print(tf.__version__)

遇到錯誤訊息

Python 3.9.10 | packaged by conda-forge | (main, Feb  1 2022, 21:27:43)

[Clang 11.1.0 ] on darwin

Type "help", "copyright", "credits" or "license" for more information.

impo>>> import tensorflow

Traceback (most recent call last):

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/__init__.py", line 22, in <module>

    from . import multiarray

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/multiarray.py", line 12, in <module>

    from . import overrides

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/overrides.py", line 7, in <module>

    from numpy.core._multiarray_umath import (

ImportError: dlopen(/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib

  Referenced from: /Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so

  Reason: tried: '/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/tensorflow/__init__.py", line 37, in <module>

    from tensorflow.python.tools import module_util as _module_util

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 37, in <module>

    from tensorflow.python.eager import context

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/tensorflow/python/eager/context.py", line 26, in <module>

    import numpy as np

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/__init__.py", line 150, in <module>

    from . import core

  File "/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in <module>

    raise ImportError(msg)

ImportError:


IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!


Importing the numpy C-extensions failed. This error can happen for

many reasons, often due to issues with your setup or how NumPy was

installed.


We have compiled some common reasons and troubleshooting tips at:


    https://numpy.org/devdocs/user/troubleshooting-importerror.html


Please note and check the following:


  * The Python version is: Python3.9 from "/Users/USER/miniforge3/bin/python"

  * The NumPy version is: "1.21.5"


and make sure that they are the versions you expect.

Please carefully study the documentation linked above for further help.


Original error was: dlopen(/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib

  Referenced from: /Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so

  Reason: tried: '/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/Users/USER/miniforge3/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)


解決方式

網路文章提到 NumPy 版本較舊,因此解決方式是在虛擬環境移除 NumPy 或者直接升級 NumPy

1. 檢查 NumPy 版本

% pip list installed | grep numpy

numpy                   1.21.5

目前 NumPy 最新版本是 1.22.0

2. 移除 NumPy 或者直接升級 NumPy

(1) 移除 numpy

pip uninstall numpy

pip install numpy  

(2) 直接升級 numpy

pip install numpy --upgrade

3. 檢查 NumPy 版本

% pip list installed | grep numpy

numpy                   1.22.3

4. 在終端機再次輸入指令

python

import tensorflow as tf

print(tf.__version__)

可以順利看到 tensorflow版本

2.8.0

 

參考資料



留言