问题
I'm trying to get tensorflow working on my MacBook pro M1. However, I keep getting the following error when trying to import: zsh: illegal hardware instruction python
I have downloaded and installed tensorflow via this link.
These were my installation steps:
- install a venv:
python3 -m venv venv
. - drag the
install_venv.sh
(which is located within the downloaded folder) file to the terminal, add-p
at the end. - select the directory of the venv as the location where tensorflow should be installed.
- activate the venv.
- type "python".
- try to import tensorflow:
import tensorflow as tf
.
I'm using 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.
回答1:
Python3 is shipped with 2 architectures in M1.
$ file $(which 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:
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.
回答2:
I've found the answer. Seemed like my terminal app was running in Rosetta. This can be changed by right clicking on the app -> get info -> disable "open with rosetta".
回答3:
I googled and found this page: https://discourse.brew.sh/t/almost-all-applications-crash-with-illegal-hardware-instruction-on-old-2009-macbook-pro/4089
I tried to uninstall vim, python@3.9 then install by:
brew install --build-from-source vim
then after installation, both vim and python3 works.
Have a try and good luck to you.
来源:https://stackoverflow.com/questions/65383338/zsh-illegal-hardware-instruction-python-when-installing-tensorflow-on-macbook