【问题标题】:Should python-dev be required to install pip是否需要 python-dev 来安装 pip
【发布时间】:2014-02-15 23:07:45
【问题描述】:

我发现很多人在用pip安装python包时遇到了麻烦,因为没有安装python-dev。主要是报错:

fatal error: Python.h: No such file or directory

所以问题是:python-dev 应该是 pip 的必需依赖项吗?或者这只是某些使用 pip 安装的软件包的问题,​​如果是这样,是否需要采取某些步骤来确保用户在安装模块时不会遇到错误?

【问题讨论】:

  • 这是一个关于某些特定(和未指定)发行版的自定义 Python 相关包的问题,​​与 Python 或编程无关。所以我相信它属于 SuperUser,或者像 AskUbuntu 这样的发行版特定网站,或者可能是发行版错误跟踪器上的错误报告。
  • @mata:但没有 python-dev 这样的东西,除非你的发行版拆分了 Python。自己构建和安装 Python,或从 python.org 运行任何安装程序,您将拥有Python.h。此外,他的问题不在于他没有编译器——他显然在 他的编译器中遇到了错误。

标签: python pip fatal-error


【解决方案1】:

我认为这实际上不属于 StackOverflow,但万一我错了……

首先,python-dev 不是 Python 的东西,它是 Ubuntu 或 Fedora 或其他发行版的东西。如果您从 python.org 下载、构建和安装 Python,或运行任何二进制安装程序,您将在适当的位置安装 Python.h。许多 linux 发行版喜欢将包拆分为子包,将您只需要构建的东西移动到 -dev 或类似的包中,对 Python 这样做绝对没有错,但它仍然是发行版正在做的事情。

其次,构建所有包不需要Python.h,只需要那些包含C扩展模块的包。由于许多包都是纯 Python 并且没有这样的扩展模块,因此发行版的 pip 包不需要其 python-dev 包是有道理的。 (就像发行版的 pip 包可能不需要 C 编译器一样。)

第三,大多数为您提供python-pip 或类似软件包的发行版也为您提供流行软件包的软件包。如果你以这种方式安装它们,要么你不需要python-dev(和一个 C 编译器),因为它们是二进制包,要么你需要它们,但它们将作为依赖项(rpm、deb、等等都有办法指定单独的“构建”和“运行”依赖项)。

但是如果你背着包管理器尝试使用pip 安装包(这是一个合理的做法),包管理器无法告诉你哪些包需要哪些依赖项,而pip 可以只告诉你 Python 包的依赖关系,所以没有什么可以强制执行的。

【讨论】:

  • 非常感谢,这个答案提供了很多见解。我不知道 python-dev 是一个与发行版相关的包(或者我会在 ubuntu 表单上发布!)Python.h 仅用于 C 扩展也是有道理的(因此是 .h,对吗?)我的主要目的提出这个问题是很多刚接触编程和 python 的人,虽然他们可能已经阅读过最好用 pip 安装 python 包,但可能会放弃并使用包管理器。对于初学者来说,它看起来很糟糕。这种看法不是用户的错,而是改进 python/发行版交互的机会!
  • @hansonap:我可以给你我的意见(请记住,Python 3.4+ 带有一个pip 引导程序,以及一个将pip 安装到每个虚拟环境中的venv 工具……) ,但实际上,我认为您应该提出这个想法,distutils/packaging SIG 或其他mailing lists。核心开发者,尤其是 Nick Coghlan,对这个问题的思考比我多得多,并且会有更好的答案——如果有什么他们没有想到的,他们越早听到越好。
  • @hansonap:但这是我的观点:修复 Python 2.x 为时已晚。在 3.4+ 中,要么使用 venv 作为部署工具(因此您的生产机器甚至不必运行 pip),或者在其中设置带有 wheels 的内部存储库(所以他们的 pip 永远不会需要编译任何东西)。无论哪种方式,您的生产机器上都不需要python-devlibxml2-dev 等。在开发机器和普通桌面上,没有理由不安装python-dev,所以就这样做吧。
  • 帮了我很多,这个答案绝对属于SO
【解决方案2】:

我检查了流行的 PyPi 包如何处理丢失的Python.h。我的第一个问题是找到构建 C 扩展的流行 PyPi 包,我通过检查 zmq/pyzmq、grpciopython-qpid-proton 解决了这个问题,我知道这是三个包。

zmq 包会打印一条通用的包罗万象的错误消息。 grpcio 包会打印一条特定的错误消息,试图提供最大的帮助。 python-qpid-proton 包只打印编译器错误。

ZMQ

    build/temp.linux-x86_64-3.7/scratch/vers.c:4:10: fatal error: zmq.h: No such file or directory                                                                                                                                           
        4 | #include "zmq.h"                                                                                                                                                                                                                 
          |          ^~~~~~~                                                                                                                                                                                                                 
    compilation terminated.                                                                                                                                                                                                                  

    error: command 'gcc' failed with exit status 1                                                                                                                                                                                           

    ************************************************                                                                                                                                                                                         
    Warning: Couldn't find an acceptable libzmq on the system.                                                                                                                                                                               

    If you expected pyzmq to link against an installed libzmq, please check to make sure:                                                                                                                                                    

        * You have a C compiler installed                                                                                                                                                                                                    
        * A development version of Python is installed (including headers)
        * A development version of ZMQ >= 3.2 is installed (including headers)
        * If ZMQ is not in a default location, supply the argument --zmq=<path>
        * If you did recently install ZMQ to a default location,
          try rebuilding the ld cache with `sudo ldconfig`
          or specify zmq's location with `--zmq=/usr/local`

https://gist.github.com/jiridanek/0fcd09f27d379ae984b84f174986093d

gRPC.io

    commands.CommandError: Diagnostics found a compilation environment issue:

    Could not find <Python.h>. This could mean the following:
      * You're on Ubuntu and haven't run `apt-get install python-dev`.
      * You're on RHEL/Fedora and haven't run `yum install python-devel` or
        `dnf install python-devel` (make sure you also have redhat-rpm-config
        installed)
      * You're on Mac OS X and the usual Python framework was somehow corrupted
        (check your environment variables or try re-installing?)
      * You're on Windows and your Python installation was somehow corrupted
        (check your environment variables or try re-installing?)

https://gist.github.com/jiridanek/9bff05ae79b0eb5207821fc524acf12a

Python Qpid 质子

cproton_wrap.c:150:21: fatal error: Python.h: No such file or directory
      # include <Python.h>
                          ^
compilation terminated.     
error: command 'gcc' failed with exit status 1

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2017-04-27
    • 2017-07-26
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多