我正在尝试在 M1 Mac 上安装 tensorflow。我一直在尝试按照 this tutorial 安装 the pre release version of tensorflow for Mac。 来自预发布存储库:
<块引用>此预发布版本为 macOS 11.0+ 提供了硬件加速的 TensorFlow 和 TensorFlow 插件。 M1 Mac 和基于 Intel 的 Mac 通过 Apple 的 ML Compute 框架支持原生硬件加速。
预发布需要 Python 3.8,所以我用它来设置我的 conda 环境。
上面的教程尝试为 M1 Mac 安装以前版本的 tensorflow,因此我调整了命令以使用我下载的适用于 M1 Mac 的 tensorflow 版本。
我一直在尝试使用 Miniforge 在虚拟环境中进行此安装,因为这是多个教程推荐用于 M1 Mac 安装的方法。来自 Miniforge 存储库:
<块引用>此存储库包含特定于 conda-forge 的 Conda 的最小安装程序。堪比 Miniconda
当我到达安装张量板 pip install tensorboard
的步骤时,安装进行到:
Building wheels for collected packages: grpcio
Building wheel for grpcio (setup.py) ... error
许多页面都出现错误,但这是最后一个块:
Traceback (most recent call last):
File "/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/src/python/grpcio/commands.py", line 282, in build_extensions
build_ext.build_ext.build_extensions(self)
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/distutils/command/build_ext.py", line 449, in build_extensions
self._build_extensions_serial()
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/distutils/command/build_ext.py", line 474, in _build_extensions_serial
self.build_extension(ext)
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/site-packages/setuptools/command/build_ext.py", line 208, in build_extension
_build_ext.build_extension(self, ext)
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/distutils/command/build_ext.py", line 528, in build_extension
objects = self.compiler.compile(sources,
File "/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/src/python/grpcio/_parallel_compile_patch.py", line 58, in _parallel_compile
multiprocessing.pool.ThreadPool(BUILD_EXT_COMPILER_JOBS).map(
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/multiprocessing/pool.py", line 771, in get
raise self._value
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
File "/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/src/python/grpcio/_parallel_compile_patch.py", line 54, in _compile_single_file
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File "/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/src/python/grpcio/commands.py", line 265, in new_compile
return old_compile(obj, src, ext, cc_args, extra_postargs,
File "/Users/bitflow/miniforge3/envs/conda-tf-venv/lib/python3.8/distutils/unixccompiler.py", line 120, in _compile
raise CompileError(msg)
distutils.errors.CompileError: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /Users/bitflow/miniforge3/envs/conda-tf-venv/bin/python3.8 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/setup.py'"'"'; __file__='"'"'/private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-install-ze40tjy3/grpcio_c9426269e87d4673a65bd63bd6af18ee/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/nq/2mtcpkp51dd149_bnk1g1t340000gn/T/pip-record-t11k_i1o/install-record.txt --single-version-externally-managed --compile --install-headers /Users/bitflow/miniforge3/envs/conda-tf-venv/include/python3.8/grpcio Check the logs for full command output.
之前有一个安装 grpcio 的步骤,声称工作成功,使用以下命令:
pip install --upgrade -t “$env/lib/python3.8/site-packages/” --no-dependencies --force "$libs/grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl"
我尝试在 python 中导入 tensorflow,但没有安装 tensorboard,唯一的错误似乎是尚未安装 tensorboard。这让我相信其余的设置都没有问题。
>>> import tensorflow
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.summary API due to missing TensorBoard installation.
在重试之前,我仔细检查并重试了安装 grpcio 的步骤。虽然 grpcio 似乎再次安装正常,但 tensorboard 安装仍然以同样的方式失败。
我真的很感激能提供任何帮助来解决这个问题。
答案 0 :(得分:1)
我最终参考了很多不同的材料来尝试让我的解决方案发挥作用。我的成功主要归功于两个主要材料:
<块引用>grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform.
出于某种原因,您从 MacOS 网站下载的 Python 版本似乎不喜欢这些文件,因此请卸载您当前的 Python 版本(如果您不知道如何操作,请单击 here)。 然后通过将其粘贴到终端中来从 xcode 安装 Python。
xcode-select --install
我必须设置一些环境变量才能使其工作:
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
为了使安装过程使用我已经在系统上获得的库,然后将其指向通过 Homebrew 安装的 OpenSSL:
CFLAGS="-I/opt/homebrew/opt/openssl/include"
LDFLAGS="-L/opt/homebrew/opt/openssl/lib"
我专注于在张量板安装期间尝试解决 grpcio 构建的问题。为此,按照上面的链接,并为了好玩而重新启动,我终于能够安装 tensorboard。
现在,如果我在 conda 环境中使用 Python,我可以导入 tensorflow 并且它工作得很好!