Answer a question

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.

Answers

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. This tutorial(https://realpython.com/intro-to-pyenv/) is helpful for getting pyenv configured properly.

Step 1.1 Use this post(https://github.com/pyenv/pyenv/issues/1446) if you have troubles running pyenv in zsh.

Step 1.2 Once you have python version 3.8.5 running which you can check by running python -V which should output:

Python 3.8.5

Step 2 Install virtualenv via pip install virtualenv

Step 2.1 Create a virtual environment by running virtualenv ENV

Step 2.2 Activate that virtual environment by running 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

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

Step 4 Type python which will bring up >>>in your terminal and type

>>> import tensorflow
>>>

If there is no 'zsh illegal hardware instruction" error you should be good to go.

Note: If you are using anaconda, the above will also work. 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).

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐