Distutils在构建python扩展时部分忽略CC环境变量

Distutils在构建python扩展时部分忽略CC环境变量,python,gcc,setuptools,distutils,Python,Gcc,Setuptools,Distutils,我正在尝试安装subprocess32 python模块(),但在distutils方面遇到了一些问题。该模块包括一个必须构建的C扩展,但是当我运行pip install.或python setup.py install时,我会得到以下输出: ... creating build creating build/temp.linux-x86_64-2.7 /non/existent/path/gcc -pthread -fPIC ... -c _posixsubprocess.c -o build

我正在尝试安装subprocess32 python模块(),但在distutils方面遇到了一些问题。该模块包括一个必须构建的C扩展,但是当我运行
pip install.
python setup.py install
时,我会得到以下输出:

...
creating build
creating build/temp.linux-x86_64-2.7
/non/existent/path/gcc -pthread -fPIC ... -c _posixsubprocess.c -o build/temp.linux-x86_64-2.7/_posixsubprocess.o
unable to execute '/non/existent/path/gcc': No such file or directory
显然,distutils出于某种原因使用了错误的gcc路径。然后,我尝试使用export CC=/correct/path/to/gcc手动指定gcc的正确路径,得到以下输出:

building '_posixsubprocess' extension
creating build/temp.linux-x86_64-2.7
/correct/path/to/gcc -fPIC -fno-strict-aliasing -g -O2 ... -c _posixsubprocess.c -o build/temp.linux-x86_64-2.7/_posixsubprocess.o
/non/existent/path/gcc -pthread -shared ... build/temp.linux-x86_64-2.7/_posixsubprocess.o -o build/lib.linux-x86_64-2.7/_posixsubprocess.so
unable to execute '/non/existent/path/gcc': No such file or directory

原来有问题的命令现在使用的是正确的路径,但它仍然试图使用错误的gcc位置来构建共享库。是否需要指定另一个环境变量来纠正此行为?

我自己编译扩展名,从
setup.py
中删除对扩展名的引用,安装包,然后将编译后的文件复制到正确的位置,从而找到了解决方案。我不打算把这个答案标记为已被接受,因为我确信有更好的方法。

我已经用你做了完全相同的实验。我花了一些时间深入研究
distutils
源代码,发现了问题所在

distutils
将在构建Python时使用链接配置。在本例中,用于构建python的gcc与用于构建扩展的gcc不同

也运行此命令请参见默认链接命令:

python2 "from distutils.sysconfig import get_config_var; print(get_config_var('LDSHARED'))"
您应该在配置的开头找到不正确的gcc,例如:

/non/existent/path/gcc -pthread -shared
第一种解决方案 设置
LDSHARED
Environment变量以使用相应的gcc路径覆盖它:

export LDSHARED="/correct/path/to/gcc -pthread -shared"
然后重建扩展,它应该可以工作

第二种解决方案(可能更好) 从构建时生成的
lib/python2.7/_sysconfigdata.py
文件中检索
LDSHARED
config

您可以修改此文件,因此无需设置环境变量


调试技巧 将
DISTUTILS\u DEBUG
环境设置为激活调试模式,以便在编译失败时可以看到回溯