我最近买了一台新的 MacBook Air M1,但我在安装 Pandas 时遇到了问题,因为预构建的二进制文件不适用于 macOS/arm64。如何在 Apple Silicon Mac 上安装 Pandas?
11 回答
也许为时已晚。但是,如果您不想使用rosetta2或moniconda,唯一对我有用的解决方案是从源代码安装
python3 -m pip install virtualenv
virtualenv -p python3.8 venv
source venv/bin/activate
pip install --upgrade pip
pip install numpy cython
git clone --depth 1 https://github.com/pandas-dev/pandas.git
cd pandas
python3 setup.py install
什么对我有用:
pip3 install cython
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 pip3 install numpy --no-use-pep517
OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 pip3 install pandas --no-use-pep517
有两种方法可以做到这一点。但是两者的基本过程是相同的。
方法一
打开一个终端窗口。运行arch
。结果应该是arm64
。现在做arch -x86_64 zsh
。这将在终端内创建一个 x86 模拟 zsh 会话。现在如果你运行arch
命令,结果应该是i386
.
现在执行pip install pandas
or pip3 install pandas
(无论哪个命令具有正确的版本)。这将起作用。执行exit
命令退出模拟的 zsh 会话。
您还可以在 x86 仿真下执行单个命令,而无需创建仿真 zsh 会话。运行arch -x86_64 pip install pandas
。这将pip
在 x86_emulation 下运行命令,但终端保持在本机 arm 环境中。
方法二
使用模拟 x86 环境的 Rosetta 2 打开终端应用程序。然后执行pip install pandas
or pip3 install pandas
(无论哪个命令具有正确的版本)这应该可以工作。
这个技巧适用于几乎所有在 ARM 架构中尝试安装失败的 python 包
使用 Rosetta 2 打开终端应用程序
- 关闭终端应用程序
- 在 Finder 中找到终端应用程序(通常位于Macintosh HD/Applications/Utilities中)
- 二次点击终端图标>>获取信息
- 选中标记为使用 Rosetta 打开的复选框
- 现在启动一个终端窗口(新的终端窗口将使用 Rosetta 仿真打开)
编辑 1:如果您在 x86 仿真下安装包,您还必须在 x86 仿真下运行您的 python 代码。如果您尝试在本机 ARM 环境中运行 python,则安装 x86 版本的软件包将引发架构不匹配错误。
这些文章中的大多数都未能解决“不想”在 Rosetta 下运行的问题。Apple 的 TensorFlow fork 在原生 M1 模式下工作,但没有 Pandas 可以与之匹配。如果您选择在 Rosetta 下运行,那么您将无法找到匹配的 TensorFlow。这是一个双输的局面。
您可以只使用--no-use-pep517
标志,并且可以毫无问题地安装 pandas。
这是我在笔记本电脑上的安装方式:
~/Documents/projects/m-proj
❯ pip install cython --no-use-pep517
Collecting cython
Using cached Cython-0.29.22-py2.py3-none-any.whl (980 kB)
Installing collected packages: cython
Successfully installed cython-0.29.22
~/Documents/projects/m-proj
❯ pip install numpy --no-use-pep517
Collecting numpy
Using cached numpy-1.20.1.zip (7.8 MB)
Using legacy 'setup.py install' for numpy, since package 'wheel' is not installed.
Installing collected packages: numpy
Running setup.py install for numpy ... done
Successfully installed numpy-1.20.1
~/Documents/projects/m-proj
❯ pip install pandas --no-use-pep517
Collecting pandas
Using cached pandas-1.2.3.tar.gz (5.5 MB)
Collecting python-dateutil>=2.7.3
Using cached python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Requirement already satisfied: pytz>=2017.3 in ./venv-trans/lib/python3.9/site-packages (from pandas) (2021.1)
Requirement already satisfied: numpy>=1.16.5 in ./venv-trans/lib/python3.9/site-packages (from pandas) (1.20.1)
Requirement already satisfied: six>=1.5 in ./venv-trans/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)
Using legacy 'setup.py install' for pandas, since package 'wheel' is not installed.
Installing collected packages: python-dateutil, pandas
Running setup.py install for pandas ... done
Successfully installed pandas-1.2.3 python-dateutil-2.8.1
Miniconda 是迄今为止最简单的选择。
brew install --cask miniforge
conda create -n myenv python=3.8
conda init zsh
conda activate
conda install pandas
如果您需要旧版本(conda-forge 中的 arm64 不可用)
brew install --cask miniforge
conda create -n myenv python=3.8
conda init zsh
conda activate
pip install cython
curl -L https://github.com/pandas-dev/pandas/archive/v1.1.2.zip --output pandas.zip
unzip pandas.zip
cd pandas-1.1.2
python3 setup.py install
rm pandas.zip
rm -rf pandas-1.1.2
- 为 OS X arm64(Apple Silicon)安装 conda miniforge
https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh
- 然后创建虚拟环境并安装你需要的包
conda create -n myenv python=3.6 conda activate myenv conda install numpy conda install pandas
查看这篇文章Apple M1 — Matplotlib、Python 和 Jupyter Lab或这篇文章(同一作者)Apple M1、Python、Pandas 和 Homebrew
也许你只需要用 Rosetta 运行终端(第二篇文章指出了这一点)
所以我找到了解决方案。显然,即使我选中了使用 Rosetta 打开术语的框,它也不是第一次这样做。我终于通过在术语上执行“arch”来发现这一点,它返回“arm64”,而不是“i386”。重新启动显然使“rosetta”选择棒,然后我能够成功地“pip3 install pandas”。当然,在原生 arm 模式下安装它会更好。感谢大家的指点。
参考此页面 https://ittone.ma/ittone/python-install-pandas-on-mac-m1-big-sur-into-multiple-virtualenv/
- 点安装cython
- git 克隆https://github.com/numpy/numpy.git
- cd numpy
- python3 setup.py 安装
- git 克隆https://github.com/pandas-dev/pandas.git
- cd熊猫
- python3 setup.py 安装
@user505117,您可能会发现这很有帮助: https ://github.com/apple/tensorflow_macos