0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【M1 Mac】anacondaにtensorflowをinstall・importできない(Could not find a version that satisfies the requirement)

Last updated at Posted at 2023-05-06

Anaconda3を使って、研究室用に機械学習を学ぶ環境を構築していた時のこと。
tensorflowのインストール・importができず3時間費やした。

結論

m1Macにはm1Mac用のtensorflowを使う必要があった
これに気づかず、時間がかかってしまった

解決までの過程

試したがうまくいかなかったこと

$ pip install tensorflow

ERROR: Could not find a version that satisfies the requirement tensorflow==2.7.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.7.0

pipのアップグレード

pip install --upgrade pip

anacondaからpythonのバージョンを3.10から3.9や3.8に下げてみるができない

スクリーンショット 2023-05-06 16.54.01.png

以下コマンドでインストールはできた

pip3 install --upgrade --ignore-installed https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl

↓参考にした

importできない問題

installできた!と思ったら、今度はimportができない状況に

import tensorflow
ImportError                               Traceback (most recent call last)
File ~/anaconda3/envs/study/lib/python3.9/site-pacages/tensorflow/python/pywrap_tensorflow.py:58
     56   sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_LOCAL)
---> 58 from tensorflow.python.pywrap_tensorflow_internal import *
     59 from tensorflow.python.pywrap_tensorflow_internal import __version__

File ~/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow_internal.py:28
     27         return _mod
---> 28 _pywrap_tensorflow_internal = swig_import_helper()
     29 del swig_import_helper

File ~/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow_internal.py:24, in swig_import_helper()
     23 try:
---> 24     _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
     25 finally:

File ~/anaconda3/envs/study/lib/python3.9/imp.py:242, in load_module(name, file, filename, details)
    241     else:
--> 242         return load_dynamic(name, filename, file)
    243 elif type_ == PKG_DIRECTORY:

File ~/anaconda3/envs/study/lib/python3.9/imp.py:342, in load_dynamic(name, path, file)
    340 spec = importlib.machinery.ModuleSpec(
    341     name=name, loader=loader, origin=path)
--> 342 return _load(spec)

ImportError: dlopen(/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 0x0006): tried: '/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (no such file), '/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[2], line 1
----> 1 import tensorflow

File ~/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/__init__.py:24
     21 import os as _os
     23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     26 try:
     27   # Add `estimator` attribute to allow access to estimator APIs via
     28   # "tf.estimator..."
     29   from tensorflow.python.estimator.api import estimator  # pylint: disable=g-import-not-at-top

File ~/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/__init__.py:49
     32 # TODO(drpng): write up instructions for editing this file in a doc and point to
     33 # the doc instead.
     34 # If you want to edit this file to expose modules in public tensorflow API, you
   (...)
     44 # go/tf-wildcard-import
     45 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
     47 import numpy as np
---> 49 from tensorflow.python import pywrap_tensorflow
     51 from tensorflow.python.tools import component_api_helper
     52 component_api_helper.package_hook(
     53     parent_package_str='tensorflow.python',
     54     child_package_str=(
     55         'tensorflow_estimator.python.estimator'))

File ~/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow.py:74
     69 except ImportError:
     70   msg = """%s\n\nFailed to load the native TensorFlow runtime.\n
     71 See https://www.tensorflow.org/install/errors\n
     72 for some common reasons and solutions.  Include the entire stack trace
     73 above this error message when asking for help.""" % traceback.format_exc()
---> 74   raise ImportError(msg)

ImportError: Traceback (most recent call last):
  File "/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/Users/username/anaconda3/envs/study/lib/python3.9/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/Users/username/anaconda3/envs/study/lib/python3.9/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: dlopen(/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 0x0006): tried: '/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (no such file), '/Users/username/anaconda3/envs/study/lib/python3.9/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

ここでm1macにはm1mac用のtensorflow-macosを使う必要があることを知る

$ conda install -c apple tensorflow-deps==2.8.0
$ python -m pip install tensorflow-macos==2.8.0
$ python -m pip install tensorflow-metal

これで解決と思いきや、

TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

protbuf(Googleが出したシリアライズフォーマット)のバージョンが高いことが原因だったみたいです
下げてあげます

(study) / ᐅ pip install protobuf==3.20.1
Collecting protobuf==3.20.1
  Downloading protobuf-3.20.1-py2.py3-none-any.whl (162 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.1/162.1 kB 2.7 MB/s eta 0:00:00
Installing collected packages: protobuf
  Attempting uninstall: protobuf
    Found existing installation: protobuf 4.22.4
    Uninstalling protobuf-4.22.4:
      Successfully uninstalled protobuf-4.22.4

importできるようになりました!

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?