1. 程式人生 > 實用技巧 >macOS Big Sur安裝TensorFlow踩坑記

macOS Big Sur安裝TensorFlow踩坑記

一,還是設定代理:

export http_proxy=http://192.168.31.168:1190 export https_proxy=http://192.168.31.168:1190

二、通過下面的命令安裝 pip 和 Virtualenv:

$ sudo easy_install pip
$ pip install --upgrade virtualenv

裝完提示:

WARNING: The script virtualenv is installed in '/Users/meetrice/Library/Python/2.7/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. 根據提示加入環境變數吧 export PATH="$PATH:/Users/meetrice/Library/Python/2.7/bin" 三、執行下面的命令來建立 Virtualenv 環境
建立 Python 2.7的環境: virtualenv --system-site-packages targetDirectory 執行結果: created virtual environment CPython2.7.16.final.0-64 in 2136ms creator CPython2macOsFramework(dest=/Users/meetrice/targetDirectory, clear=False, no_vcs_ignore=False, global=True) seeder FromAppData(download=False, pip=bundle, wheel=bundle, setuptools=bundle, via=copy, app_data_dir=/Users/meetrice/Library/ Application Support/virtualenv) added seed packages: pip==20.3.1, setuptools==44.1.1, wheel==0.36.1 activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator 這表示建立成功! 建立 Python 3.n 的環境: virtualenv --system-site-packages -p python3 執行結果出現異常 : RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3' 於是單獨執行python3命令: python3 出現提示 You have not agreed to the Xcode license agreements. You must agree to both license agreements below in order to use Xcode. Hit the Return key to view the license agreements at '/Applications/Xcode.app/Contents/Resources/English.lproj/License.rtf' 根據提示輸入回車,此時會顯示授權協議 不斷的按空格鍵直到結尾,出現 By typing 'agree' you are agreeing to the terms of the software license agreements. Type 'print' to print them or anything else t o cancel, [agree, print, cancel] 輸入 "agree"回車 這此python3就已經可以正常使用 再一次安裝python 3的虛擬環境 virtualenv --system-site-packages -p python3 targetDirectory 執行後: created virtual environment CPython3.8.2.final.0-64 in 2232ms creator CPython3macOsFramework(dest=/Users/meetrice/targetDirectory, clear=False, no_vcs_ignore=False, global=True) seeder FromAppData(download=False, pip=bundle, wheel=bundle, setuptools=bundle, via=copy, app_data_dir=/Users/meetrice/Library/ Application Support/virtualenv) added seed packages: pip==20.3.1, setuptools==51.0.0, wheel==0.36.1 activators PythonActivator,FishActivator,XonshActivator,CShellActivator,PowerShellActivator,BashActivator 成功! 四、啟用 Virtualenv 環境
cd targetDirectory source ./bin/activate (targetDirectory)$ easy_install -U pip

(targetDirectory)$ pip install --upgrade tensorflow # 對應 Python 2.7
(targetDirectory)$ pip3 install --upgrade tensorflow # 對應 Python 3.n

五,驗證tensorFlow,執行HelloWorld

在虛擬環境中的當前目錄,建立helloworld.py

(targetDirectory) meetrice@mbp targetDirectory % nano helloworld.py 在編輯介面輸入以下內容:
import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)
然後ctrl+x Y,回車儲存 然後執行 python helloworld.py 出現結果: 2020-12-27 14:34:26.911207: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices n ot set 2020-12-27 14:34:26.918930: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. Hello, TensorFlow! HelloWorld 成功!