Is there some way to pass build_ext
options to pip install to alter how an extension included in a package is compiled? (Yes, I know that one can download the source and build/install with a custom setup.cfg
, but I'm curious whether it is possible to pass options that can be specified in setup.cfg
directly through pip.)

- 1,371
- 2
- 12
- 27
3 Answers
It's possible using pip --global-option=build_ext
.
For example this is requirements.txt
for Pillow with PNG and JPEG support and no other external libraries:
pillow \
--global-option="build_ext" \
--global-option="--enable-zlib" \
--global-option="--enable-jpeg" \
--global-option="--disable-tiff" \
--global-option="--disable-freetype" \
--global-option="--disable-tcl" \
--global-option="--disable-tk" \
--global-option="--disable-lcms" \
--global-option="--disable-webp" \
--global-option="--disable-webpmux" \
--global-option="--disable-jpeg2000"
This is really an abuse of pip --global-option
, inspired by this answer, as build_ext
is a pip command and not really a global pip option. But this would make pip to execute two commands — first build_ext
and then install
— like this:
pip \
build_ext \
--enable-zlib --enable-jpeg \
--disable-tiff --disable-freetype --disable-tcl --disable-tk \
--disable-lcms --disable-webp --disable-webpmux --disable-jpeg2000 \
install pillow
-
Excellent. Works like charm. – Jaap Versteegh Aug 20 '15 at 13:13
-
1pip build_ext doesn't work for me at this time. – imba-tjd May 16 '21 at 06:22
You can create .pydistutils.cfg
file in your home directory and override build options like you could do with custom setup.cfg
, but without need to unpack package first.
So, for example, you can write something like this to alter include & lib search path:
[build_ext]
include_dirs=/usr/local/include
library_dirs=/usr/local/lib64
rpath=/usr/local/lib64

- 5,904
- 6
- 26
- 28

- 7,196
- 3
- 46
- 68
-
14Funny that I've googled up my own answer after completely forgetting I once knew this. – toriningen Aug 14 '17 at 19:52
I searched for such option in pip
and found none (searched the source too).
I don't think there is no such option in easy_install/setuptools too.
The only solution I see is an old-school way:
download / unpack / setup.py build_ext [options] / setup.py install
.

- 15,383
- 5
- 56
- 73