在 M1 上的 Conda 环境中安装 Tensorflow 时 Tensorboard 安装失败 Mac

Tensorboard Install Fails while Installing Tensorflow in Conda Environment on M1 Mac

我正在尝试在 M1 Mac 上安装 tensorflow。我一直在努力关注 this tutorial to install the pre release version of tensorflow for Mac。 来自预发布仓库:

This pre-release delivers hardware-accelerated TensorFlow and TensorFlow Addons for macOS 11.0+. Native hardware acceleration is supported on M1 Macs and Intel-based Macs through Apple’s ML Compute framework.

预发布版需要 Python 3.8,这就是我设置 conda 环境的原因。

上面的教程尝试为 M1 Mac 安装以前版本的 tensorflow,因此我调整了命令以使用我下载的 M1 Mac 的 tensorflow 版本。

我一直在尝试使用 Miniforge 在虚拟环境中进行此安装,因为这是多个教程为 M1 Mac 安装推荐的方法。来自 Miniforge 仓库:

This repository holds a minimal installer for Conda specific to conda-forge. It is comparable to Miniconda


当我到达安装 tensorboard 的步骤时,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 安装仍然以同样的方式失败。

如果能提供解决此问题的任何帮助,我将不胜感激。

我最终参考了很多不同的材料来尝试让我的解决方案发挥作用。我的成功归功于两个主要材料:

grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform.

For some reason the version of Python that you download from the website for MacOS doesnt seem to like these files, so uninstall your current version of Python (if you dont know how to do that click here). And then install the Python from xcode by pasting this into your terminal.

xcode-select --install

I had to set a few environment variables to get it to work:

GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1

to make the installation process use the libraries I've already got on the system, and then the following to point it at OpenSSL installed via Homebrew:

CFLAGS="-I/opt/homebrew/opt/openssl/include"
LDFLAGS="-L/opt/homebrew/opt/openssl/lib"

我专注于尝试解决在张量板安装期间构建 grpcio 的问题。为此,按照上面的链接,为了好玩而重新启动,我终于能够安装 tensorboard。

现在,如果我在我的 conda 环境中使用 Python,我可以导入 tensorflow,它工作得很好!