运行python setup.py install时强制编译器 [英] Force compiler when running python setup.py install
问题描述
在运行python setup.py install
时,是否有一种方法可以显式强制编译器构建Cython扩展?其中setup.py
的格式为:
Is there a way to explicitly force the compiler for building Cython extensions when running python setup.py install
? Where setup.py
is of the form:
import os.path
import numpy as np
from setuptools import setup, find_packages, Extension
from Cython.Distutils import build_ext
setup(name='test',
packages=find_packages(),
cmdclass={'build_ext': build_ext},
ext_modules = [ Extension("test.func", ["test/func.pyx"]) ],
include_dirs=[np.get_include()]
)
我正在尝试使用Anaconda 3.16,Python 3.4,setuptools 18,Numpy 1.9和Cython 0.24在Windows 8.1 x64上安装软件包.部署脚本改编自Cython Wiki 和此堆栈溢出答案.
I'm trying to install a package on Windows 8.1 x64 using Anaconda 3.16, Python 3.4, setuptools 18, Numpy 1.9 and Cython 0.24. The deployment script is adapted from the Cython wiki and this Stack Overflow answer.
Makefile.bat
:: create and activate a virtual environement with conda
conda create --yes -n test_env cython setuptools=18 pywin32 libpython numpy=1.9 python=3
call activate test_env
:: activate the MS SDK compiler as explained in the Cython wiki
cd C:\Program Files\Microsoft SDKs\Windows\v7.1\
set MSSdk=1
set DISTUTILS_USE_SDK=1
@call .\Bin\SetEnv /x64 /release
cd C:\test
python setup.py install
问题在于,在这种情况下,setup.py install
仍使用conda附带的mingw编译器,而不是MS Windows SDK 7.1编译器.
The problem is that in this case setup.py install
still used the mingw compiler included with conda instead of the MS Windows SDK 7.1 one.
-
所以
DISTUTILS_USE_SDK=1
和MSSdk=1
似乎对buid没有影响.我不确定在conda virtualenv中激活MS SDK是否会成为问题.
So the
DISTUTILS_USE_SDK=1
andMSSdk=1
don't seem to have an impact on the buid. I'm not sure if activating the MS SDK from within a conda virtualenv might be an issue here.
运行python setup.py build_ext --compiler=msvc
使用MS编译器正确构建扩展,但是随后运行setup.py install
,再次使用mingw重新编译它.同样适用于python setup.py build --compiler=msvc
.
Running python setup.py build_ext --compiler=msvc
correctly builds the extension with the MS compiler, but subsequently running the setup.py install
, recompiles it with mingw again. Same applies to python setup.py build --compiler=msvc
.
也尝试按照上面的答案中的讨论运行%COMSPEC% /E:ON /V:ON /K "%PROGRAMFILES%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd"
,但是对我来说,这会产生一个新的终端提示符,显示为黄色,并停止安装过程.
Also tried running %COMSPEC% /E:ON /V:ON /K "%PROGRAMFILES%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd"
as discussed in the answer linked above, but for me this produces a new terminal prompt, coloured in yellow, and stops the install process.
是否有一种例如通过编辑setup.py
来强制编译器构建此程序包的方法?
Is there a way of forcing the compiler for building this package, for instance, by editing the setup.py
?
推荐答案
您可以在一个名为setup.cfg
的单独文件中(与您的setup.py
平行放置)为distutils提供(默认)命令行参数.有关更多信息,请参见文档.要设置编译器,请使用以下命令:
You can provide (default) command line arguments for distutils in a separate file called setup.cfg
(placed parallel to your setup.py
). See the docs for more information. To set the compiler use something like:
[build]
compiler=msvc
现在调用python setup.py build
等效于调用python setup.py build --compiler=msvc
. (您仍然可以通过调用python setup.py build --compiler=someothercompiler
来指示distutils使用其他编译器)
Now calling python setup.py build
is equivalent to calling python setup.py build --compiler=msvc
. (You can still direct distutils to use an other complier by calling python setup.py build --compiler=someothercompiler
)
现在,您已经(成功地指示distutils使用 a msvc编译器.不幸的是,没有选择告诉它要使用哪个 msvc编译器.基本上有两个选择:
Now you have (successfully directed distutils to use a msvc compiler. Unfortunately there is no option to tell it which msvc compiler to use. Basically there are two options:
一个:不执行任何操作,并且distutils会尝试找到vcvarsall.bat
并将其用于设置环境. vcvarsall.bat
(以及为其设置环境的编译器)是Visual Studio的一部分,因此您必须已安装它才能使其工作.
One: Do nothing and distutils will try to locate vcvarsall.bat
and use that to setup an environment. vcvarsall.bat
(and the compiler it sets the environment up for) are part of Visual Studio, so you have to have installed that for it to work.
两个:安装Windows SDK,并告诉distutils使用它.请注意,名称DISUTILS_USE_SDK
相当容易引起误解(至少在我看来).实际上,它并没有告诉distutils使用SDK(它是setenv.bat
)来设置环境,而是意味着distutils应该假定已经设置了环境.这就是为什么您必须使用OP中显示的Makefile.bat
的原因.
Two: Install the Windows SDK and tell distutils to use that. Be aware that the name DISUTILS_USE_SDK
is rather missleading (at least in my opinion). It does NOT in fact tell distutils to use the SDK (and it's setenv.bat
) to setup an environment, rather it means that distutils should assume the environment has already been set up. That is why you have to use some kind of Makefile.bat
as you have shown in the OP.
侧面说明: VisualStudio或Windows SDK的特定版本取决于目标python版本.
Side Note: The specific version of VisualStudio or the Windows SDK depends on the targeted python version.
这篇关于运行python setup.py install时强制编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!