pip安装numpy出现问题 - RuntimeError: Broken toolchain: cannot link a simple C program

142
我想在虚拟环境中安装numpy(以及scipy和matplotlib)。但我一直遇到这些错误:
RuntimeError: Broken toolchain: cannot link a simple C program

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1

我已经安装了Xcode的命令行工具

$ which gcc
/usr/bin/gcc
$ which cc
/usr/bin/cc

我的电脑运行的是Mac OSX 10.9系统,使用的是brew安装的Python。

编辑
是的,我正在尝试使用pip进行安装。
整个回溯内容太大了(>400行)。

这里是其中的一部分:

C compiler: cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe



compile options: '-Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -Inumpy/core/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c'

cc: _configtest.c

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

failure.

removing: _configtest.c _configtest.o

Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/setup.py", line 192, in <module>

    setup_package()

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/setup.py", line 185, in setup_package

    configuration=configuration )

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/core.py", line 169, in setup

    return old_setup(**new_attr)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup

    dist.run_commands()

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands

    self.run_command(cmd)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command

    cmd_obj.run()

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/command/egg_info.py", line 10, in run

    self.run_command("build_src")

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command

    self.distribution.run_command(command)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command

    cmd_obj.run()

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/command/build_src.py", line 153, in run

    self.build_sources()

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/command/build_src.py", line 164, in build_sources

    self.build_library_sources(*libname_info)

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/command/build_src.py", line 299, in build_library_sources

    sources = self.generate_sources(sources, (lib_name, build_info))

  File "/Users/bdhammel/Documents/research_programming/julia_env/build/numpy/numpy/distutils/command/build_src.py", line 386, in generate_sources

    source = func(extension, build_dir)

  File "numpy/core/setup.py", line 674, in get_mathlib_info

    raise RuntimeError("Broken toolchain: cannot link a simple C program")

RuntimeError: Broken toolchain: cannot link a simple C program

6
哈哈,“不能链接一个简单的C程序”。这么嘴利! - Cameron Hudson
准备好迎接202X年会有大量新手自然而然地在他们喜欢的虚拟环境中安装python2.x,然后执行pip install mypackage,导致pip将它们带到这里。我认为python2的强制退出有点过于激进。一个具有python2和pip的工作虚拟机,能够正常运行5年或10年前编写的旧版python程序,将会得到很多硬币的交易。 - Eric Leschinski
13个回答

177

对于 Docker(Alpine)和 Python 3.x,这对我有效:

RUN apk update
RUN apk add make automake gcc g++ subversion python3-dev

42
对于使用Docker Python3 Alpine镜像的任何人,这是解决方案。 - xssChauhan
5
我需要先执行一个“apk update”命令,否则会收到“ERROR: unsatisfiable constraints”的错误提示。 - yair
这也是docker python2 alpine镜像的解决方案。 - Eduard
11
我能够在alpine上仅使用gcc g++ make python3-dev来安装numpy/nmslib。这个特定问题的原因似乎是缺少c++编译器g++(从错误信息看,gccmake已经被安装了)。其他重要的numpy构建部分是由python3-dev提供的Python3头文件。 - ThisGuyCantEven
2
不确定是否需要 subversion。官方的 Python Docker 基于 buildpack-deps,其中没有列出 subversion。 - phi
帮助我的软件包列表是 g++ jpeg-dev zlib-dev libjpeg make。不确定它们每个的作用是什么。 - havryliuk

84
虽然它很丑,但似乎可以工作。
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install --upgrade numpy

请注意,如果您在安装除numpy以外的软件包(例如lxml)时遇到此错误,请在命令末尾指定该软件包名称,而不是numpy
我看到有人在安装gem时遇到类似的问题。 在Mavericks和Xcode 5.1上安装Ruby Gem Json失败-未知参数:'-multiply_definedsuppress' 这只是一个临时解决方案,某个时候需要修复编译器选项。

1
@Ben:顺便说一下,我也遇到了同样的问题(没有使用brew)。这是在更新Xcode到5.1之后出现的。然而丑陋,但目前看来这似乎是答案。 - orome
2
XCode 5.1发布说明中有一个解释。 - badzil
在Yosemite下使用最新的Xcode,是否仍需要unused-command-line-argument-hard-error-in-future?似乎没有它也能正常工作(至少对于我尝试过的软件包而言)。 - orome
1
有没有Windows的解决方案? - Zack Plauché

17

问题在于你无法编译。

首先,请确保你已经接受了Xcode的新条款和条件。要做到这一点,只需打开xCode并接受即可。

然后,尝试使用以下命令安装gcc:

brew install gcc

最后,请尝试使用

Numpy安装
pip install numpy

希望这能有所帮助。


这对我有用。问题是我没有打开Xcode并接受条款和条件。 - Stephens
对于那些运行 Big Sur Beta 的用户:(1) 下载并安装 Xcode 12.2 beta 3 的命令行工具 (2) 在终端中输入 sudo xcode-select --switch /Library/Developer/CommandLineTools (3) brew install gcc (4) pip install numpy - felipe

9
如果您不想使用sudo(这样使用venv时权限和其他类似的东西就会被保留),可以将ARCHFLAGS声明添加到您的.bash_profile文件中,然后像平常一样运行。在Mavericks和Xcode 5.1中,我用venv成功实现了这个操作。
在~/.bash_profile文件中:
export ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future 然后只需运行以下命令即可: pip install --upgrade numpy

5
上面的sudo是用于系统范围安装的,如果在虚拟环境中运行则不需要使用。 - Eren Güven

8
如果你正在运行Linux发行版,特别是如果你看到类似于“sh:gcc:command not found”这样的日志行,你可能需要一个C编译器。你可以按照这里的说明进行操作,以下是我总结的步骤:
  • Fedora、Red Hat、CentOS或Scientific Linux

    # yum groupinstall 'Development Tools'

  • Debian或Ubuntu Linux

    $ sudo apt-get update $ sudo apt-get install build-essential manpages-dev

然后你可以尝试重新运行:
sudo pip install numpy

6
在我的情况下,这发生在docker构建过程中。问题在于基础镜像没有固定到特定的Python版本,而numpy无法与新版本进行编译。
FROM python:3-slim  # BAD

我将其更改为以下内容后,它起作用了:

FROM python:3.8-slim  # GOOD

记得修复你的版本!:-)


今天我也遇到了同样的问题。看起来他们在3.9.0中删除了C编译器和相关的make工具。 - PF1

5

我只需要打开XCode并接受协议,让它安装工具即可。然后返回PyCharm,再次安装numpy,没有遇到任何问题。


5

在 Fedora 22 上,这个问题可以通过更新 pip 自身得到解决: sudo pip install --upgrade pip


同样的解决方案也适用于在Big Sur上运行的MacOS的问题。 - Jack L.

4
对于遇到类似问题的 Fedora 用户,尝试安装以下软件包:
(如果未使用 python3,请使用 python-devel 和 pip 替代 pip3)
sudo dnf install python3-devel
sudo dnf install make automake gcc gcc-c++ gcc-gfortran
sudo dnf install redhat-rpm-config
sudo dnf install subversion

然后尝试。
sudo pip3 install numpy

3

在某些情况下,当OS X升级XCode时,XCode会要求用户(具有管理员权限)接受新的许可证。在许可证被接受之前,尝试编译和链接代码时,clang和gcc将发出错误消息。或至少是python包。

如果您启动XCode并接受许可证,则不再显示错误。至少对我而言是这样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接