[英]"zsh: illegal hardware instruction python" when installing Tensorflow on macbook pro M1 [duplicate]
I'm trying to get tensorflow working on my MacBook pro M1.我正在尝试让 tensorflow 在我的 MacBook pro M1 上运行。 However, I keep getting the following error when trying to import:
zsh: illegal hardware instruction python
但是,我在尝试导入时不断收到以下错误:
zsh: illegal hardware instruction python
I have downloaded and installed tensorflow via this link .我已经通过此链接下载并安装了 tensorflow。
These were my installation steps:这些是我的安装步骤:
python3 -m venv venv
.python3 -m venv venv
。install_venv.sh
(which is located within the downloaded folder) file to the terminal, add -p
at the end.install_venv.sh
(位于下载的文件夹中)文件拖到终端,在末尾添加-p
。import tensorflow as tf
.import tensorflow as tf
。 I'm using Python 3.8.2.我正在使用 Python 3.8.2。
I've seen some tutorials where this exact method does work, so I don't know what's the issue here.我看过一些教程,其中这个确切的方法确实有效,所以我不知道这里有什么问题。
This worked for me after trying a bunch of solutions to no avail.在尝试了一堆解决方案无济于事之后,这对我有用。
Step 1 Using pyenv install python version 3.8.5 and set it as your default python version.步骤 1 使用 pyenv 安装 python 版本 3.8.5 并将其设置为默认版本 python。 This tutorial( https://realpython.com/intro-to-pyenv/ ) is helpful for getting pyenv configured properly.
本教程 ( https://realpython.com/intro-to-pyenv/ ) 有助于正确配置 pyenv。
Step 1.1 Use this post( https://github.com/pyenv/pyenv/issues/1446 ) if you have troubles running pyenv in zsh.步骤 1.1 如果您在 zsh 中运行 pyenv 时遇到问题,请使用这篇文章 ( https://github.com/pyenv/pyenv/issues/1446 )。
Step 1.2 Once you have python version 3.8.5 running which you can check by running python -V
which should output:步骤 1.2 运行 python 版本 3.8.5 后,您可以通过运行
python -V
检查它应该是 output:
Python 3.8.5
Step 2 Install virtualenv via pip install virtualenv
Step 2 通过
pip install virtualenv
Step 2.1 Create a virtual environment by running virtualenv ENV
步骤 2.1 通过运行
virtualenv ENV
创建虚拟环境
Step 2.2 Activate that virtual environment by running source ENV/bin/activate
步骤 2.2 通过运行
source ENV/bin/activate
激活该虚拟环境
Step 3 Install the tensorflow wheel called tensorflow-2.4.1-py3-none-any.whl
located at this public google drive link https://drive.google.com/drive/folders/1oSipZLnoeQB0Awz8U68KYeCPsULy_dQ7第 3 步安装名为
tensorflow-2.4.1-py3-none-any.whl
位于这个公共谷歌驱动器链接https://drive.google.com/drive/folders/1oSipZLnoeQB0Awz8U68KYeCPsULy_dQ7
Step 3.1 Assuming you simply installed the wheel to downloads run pip install ~/Downloads/tensorflow-2.4.1-py3-none-any.whl
in your activated virtual environment步骤 3.1 假设您只是安装了下载轮
pip install ~/Downloads/tensorflow-2.4.1-py3-none-any.whl
在您激活的虚拟环境中
Step 4 Type python which will bring up >>>
in your terminal and type第 4 步键入 python,这将在您的终端中调出
>>>
并键入
>>> import tensorflow
>>>
If there is no 'zsh illegal hardware instruction" error you should be good to go.如果没有'zsh illegal hardware instruction'的错误你应该是go就好了。
Note: If you are using anaconda, the above will also work.注意:如果您使用的是 anaconda,上述方法也适用。 You can skip the virtual env steps (assuming you have a virtual env activated through Conda) and just go straight to the
pip install
as mentioned above (steps 3 and later).您可以跳过虚拟环境步骤(假设您有一个通过 Conda 激活的虚拟环境),只需 go 直接进入
pip install
,如上所述(第 3 步及更高版本)。
Python3 is shipped with 2 architectures in M1. Python3 在 M1 中附带了 2 种架构。
$ file $(which python3)
# If you installed python through Homebrew or Anaconda, deactivate your conda env, then run this line instead:
# $ file $(which /usr/bin/python3)
/usr/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/usr/bin/python3 (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python3 (for architecture arm64e): Mach-O 64-bit executable arm64e
Here, it's very important to specify which one to use.在这里,指定使用哪一个非常重要。 You can do this by installing the script like this:
您可以通过安装这样的脚本来做到这一点:
arch -arm64 bash install_venv.sh my_tf_env
if you have multiple python installations, use:如果您有多个 python 安装,请使用:
arch -arm64 bash install_venv.sh --python=/usr/bin/python3 my_tf_env
You can replace my_tf_env
with any other name/path you choose.您可以将
my_tf_env
替换为您选择的任何其他名称/路径。
I had the same issue我遇到过同样的问题
This is because of M1 chip.这是因为M1芯片。 Now there is a pre-release that delivers hardware-accelerated TensorFlow and TensorFlow Addons for macOS 11.0+.
现在有一个预发布版本,它为 macOS 11.0+ 提供硬件加速的 TensorFlow 和 TensorFlow Addons。 Native hardware acceleration is supported on M1 Macs and Intel-based Macs through Apple's ML Compute framework.
通过 Apple 的 ML Compute 框架,M1 Mac 和基于 Intel 的 Mac 支持本机硬件加速。
You need to install the TensorFlow that supports M1 chip Simply pull this tensorflow macos repository and run the./scripts/download_and_install.sh您需要安装支持 M1 芯片的 TensorFlow 只需拉出这个tensorflow macos 存储库并运行./scripts/download_and_install.sh
I'm trying to get tensorflow working on my MacBook pro M1.我正在尝试让 tensorflow 在我的 MacBook pro M1 上工作。 However, I keep getting the following error when trying to import:
zsh: illegal hardware instruction python
但是,尝试导入时,我不断收到以下错误:
zsh: illegal hardware instruction python
I have downloaded and installed tensorflow via this link .我已经通过这个链接下载并安装了 tensorflow。
These were my installation steps:这些是我的安装步骤:
python3 -m venv venv
.python3 -m venv venv
。install_venv.sh
(which is located within the downloaded folder) file to the terminal, add -p
at the end.install_venv.sh
(位于下载的文件夹中)文件拖到终端,在末尾添加-p
。import tensorflow as tf
.import tensorflow as tf
。 I'm using Python 3.8.2.我正在使用 Python 3.8.2。
I've seen some tutorials where this exact method does work, so I don't know what's the issue here.我看过一些教程,这种确切的方法确实有效,所以我不知道这里有什么问题。
I've found the answer.我找到了答案。 Seemed like my terminal app was running in Rosetta.
好像我的终端应用程序正在 Rosetta 中运行。 This can be changed by right clicking on the app -> get info -> disable "open with rosetta".
这可以通过右键单击应用程序 -> 获取信息 -> 禁用“使用 Rosetta 打开”来更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.