Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force compiler when running python setup.py install

Tags:

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()]  ) 

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 

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.

  • So the DISTUTILS_USE_SDK=1 and MSSdk=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.

  • 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.

  • 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.

Is there a way of forcing the compiler for building this package, for instance, by editing the setup.py?

like image 277
rth Avatar asked Oct 25 '15 15:10

rth


People also ask

Does pip install use setup py?

@joeforker, pip uses setup.py behind the scenes.

What is python setup py install?

The setup.py is a Python script typically included with Python-written libraries or apps. Its objective is to ensure that the program is installed correctly. With the aid of pip , we can use the setup.py to install any module without having to call setup.py directly. The setup.py is a standard Python file.

How do I run file setup py?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.


2 Answers

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 

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)

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:

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.

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.

Side Note: The specific version of VisualStudio or the Windows SDK depends on the targeted python version.

like image 96
PeterE Avatar answered Sep 19 '22 12:09

PeterE


As a remark: on linux, you can use many of the autoconf environment variables. For the compiler

CC=mpicc python setup.py build_ext -i 
like image 25
Fred Schoen Avatar answered Sep 16 '22 12:09

Fred Schoen