是否需要python-dev来安装pip?

问题描述 投票:15回答:2

我发现很多人用pip安装python包的时候很麻烦,因为python-dev没有安装。主要是出错。

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

那么问题来了: python-dev应该是pip的一个必要依赖项吗?还是说这只是某些包在用pip安装时的问题,如果是这样,是否有一定的措施来保证用户在安装你的模块时不会遇到这个错误?

python pip fatal-error
2个回答
20
投票

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

第一: python-dev 不是Python的东西,而是Ubuntu或Fedora或其他发行版的东西。如果你下载、构建和安装 Python,或者运行 python.org 上的任何二进制安装程序,你会获得 Python.h 安装在一个合适的地方。许多linux发行版喜欢把包拆成子包,把你只需要构建的东西移到一个 -dev 或类似的包,对Python这样做绝对没有错,但这还是发行版在做的事情。

第二个问题。Python.h 不需要用于构建所有的软件包,只有那些包含C扩展模块的软件包才需要。由于许多软件包是纯Python的,而没有这样的扩展模块,因此发行版的 pip 包就不需要它的 python-dev 包。(就像一个发行版的 pip 软件包可能不需要C编译器)。)

第三,大多数发行版都会给你一个 python-pip 或类似的包也会给你流行包的包。如果你以这种方式安装,要么你就不需要 python-dev 和一个C编译器),因为它们是二进制包,或者你会需要它们,但它们会被作为依赖关系拉进来(rpm、deb等都有办法指定单独的 "构建 "和 "运行 "依赖关系)。

但是,如果你背着包管理器,试图安装带有 pip (这是一个合理的事情),包管理器不能告诉你哪些包需要什么依赖,而 pip 只能告诉你Python包的依赖性,所以没有什么可以强制执行的。


0
投票

我检查了流行的PyPi包如何处理丢失的 Python.h. 我的第一个问题是找到流行的PyPi包,建立一个C扩展,我通过检查 zmqpyzmq, 佣金蟒蛇-qpid-质子,我知道有三个包在这样做。

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.comjiridanek0fcd09f27d379ae984b84f174986093d。

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.comjiridanek9bff05ae79b0eb5207821fc524acf12a。

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
© www.soinside.com 2019 - 2024. All rights reserved.