1- =================================================
2- Building Binary Wheels for Windows using Appveyor
3- =================================================
1+ =================================
2+ Supporting Windows using Appveyor
3+ =================================
44
55:Page Status: Incomplete
6- :Last Reviewed: 2014-09-27
6+ :Last Reviewed: 2015-12-03
77
88This section covers how to use the free `Appveyor `_ continuous integration
9- service to build Windows-targeted binary wheels for your project.
9+ service to provide Windows support for your project. This includes testing
10+ the code on Windows, and building Windows-targeted binaries for projects
11+ that use C extensions.
1012
1113.. contents :: Contents
1214 :local:
@@ -15,19 +17,23 @@ service to build Windows-targeted binary wheels for your project.
1517Background
1618==========
1719
18- Windows users typically do not have access to a C compiler, and therefore are
19- reliant on projects that use C extensions distributing binary wheels on PyPI in
20- order for the distribution to be installable via ``pip install dist ``.
21- However, it is often the case that projects which are intended to be
22- cross-platform are developed on Unix, and so the project developers *also * have
23- the problem of lack of access to a Windows compiler.
20+ Many projects are developed on Unix by default, and providing Windows support
21+ can be a challenge, because setting up a suitable Windows test environment is
22+ non-trivial, and may require buying software licenses.
2423
2524The Appveyor service is a continuous integration service, much like the
2625better-known `Travis `_ service that is commonly used for testing by projects
2726hosted on `Github `_. However, unlike Travis, the build workers on Appveyor are
2827Windows hosts and have the necessary compilers installed to build Python
2928extensions.
3029
30+ Windows users typically do not have access to a C compiler, and therefore are
31+ reliant on projects that use C extensions distributing binary wheels on PyPI in
32+ order for the distribution to be installable via ``pip install <dist> ``. By
33+ using Appveyor as a build service (even if not using it for testing) it is
34+ possible for projects without a dedicated Windows environment to provide
35+ Windows-targeted binaries.
36+
3137Setting Up
3238==========
3339
@@ -52,6 +58,13 @@ In order to define how Appveyor should build your project, you need to add an
5258in the file are covered in the Appveyor documentation. This guide will provide
5359the details necessary to set up wheel builds.
5460
61+ Appveyor includes by default all of the compiler toolchains needed to build
62+ extensions for Python. For Python 2.7, 3.5+ and 32-bit versions of 3.3 and 3.4,
63+ the tools work out of the box. But for 64-bit versions of Python 3.3 and 3.4,
64+ there is a small amount of additional configuration needed to let distutils
65+ know where to find the 64-bit compilers. (From 3.5 onwards, the version of
66+ Visual Studio used includes 64-bit compilers with no additional setup).
67+
5568appveyor.yml
5669------------
5770
@@ -65,54 +78,59 @@ The ``appveyor.yml`` file must be located in the root directory of your
6578project. It is in ``YAML `` format, and consists of a number of sections.
6679
6780The ``environment `` section is the key to defining the Python versions for
68- which your wheels will be created. Appveyor comes with Python 2.7, 3.3 and 3.4
69- installed, in both 32-bit and 64-bit builds. The example file builds for all of
70- these environments.
71-
72- The ``install `` section installs any additional software that the project may
73- require. The supplied code installs ``pip `` (if needed) and ``wheel ``. Projects
74- may wish to customise this code in certain circumstances (for example, to install
75- additional build packages such as ``Cython ``, or test tools such as ``tox ``).
81+ which your wheels will be created. Appveyor comes with Python 2.6, 2.7, 3.3,
82+ 3.4 and 3.5 installed, in both 32-bit and 64-bit builds. The example file
83+ builds for all of these environments except Python 2.6. Installing for Python
84+ 2.6 is more complex, as it does not come with pip included. We don't support
85+ 2.6 in this document (as Windows users still using Python 2 are generally able
86+ to move to Python 2.7 without too much difficulty).
87+
88+ The ``install `` section uses pip to install any additional software that the
89+ project may require. The only requirement for building wheels is the ``wheel ``
90+ project, but projects may wish to customise this code in certain circumstances
91+ (for example, to install additional build packages such as ``Cython ``, or test
92+ tools such as ``tox ``).
7693
7794The ``build `` section simply switches off builds - there is no build step needed
7895for Python, unlike languages like ``C# ``.
7996
80- The ``test_script `` section is technically not needed. The supplied file runs
81- your test suite using ``setup.py test ``. You may wish to use another test tool
82- such as ``tox `` or ``py.test ``. Or you could skip the test (but why would you,
83- unless your tests are expected to fail on Windows?) by replacing the script with
84- a simple ``echo Skipped `` command.
97+ The main sections that will need to be tailored to your project are ``test_script ``
98+ and ``after_test ``.
99+
100+ The ``test_script `` section is where you will run your project's tests. The
101+ supplied file runs your test suite using ``setup.py test ``. If you are only
102+ interested in building wheels, and not in running your tests on Windows, you
103+ can replace this section with a dummy command such as ``echo Skipped Tests ``.
104+ You may wish to use another test tool, such as ``nose `` or ``py.test ``. Or you
105+ may wish to use a test driver like ``tox `` - however if you are using ``tox ``
106+ there are some additional configuration changes you will need to consider,
107+ which are described below.
85108
86- The ``after_test `` command is where the wheels are built. Assuming your project
87- uses the recommended tools (specifically, ``setuptools ``) then the
88- ``setup.py bdist_wheel `` command will build your wheels.
109+ The ``after_test `` runs once your tests have completed, and so is where the
110+ wheels should be built. Assuming your project uses the recommended tools
111+ (specifically, ``setuptools ``) then the ``setup.py bdist_wheel `` command
112+ will build your wheels.
89113
90114Note that wheels will only be built if your tests succeed. If you expect your
91115tests to fail on Windows, you can skip them as described above.
92116
93117
94- Support scripts
95- ---------------
118+ Support script
119+ --------------
96120
97- The ``appveyor.yml `` file relies on two support scripts. The code assumes that
98- these will be placed in a subdirectory named ``appveyor `` at the root of your
99- project.
121+ The ``appveyor.yml `` file relies on a single support script, which sets up the
122+ environment to use the SDK compiler for 64-bit builds on Python 3.3 and 3.4.
123+ For projects which do not need a compiler, or which don't support 3.3 or 3.4 on
124+ 64-bit Windows, only the ``appveyor.yml `` file is needed.
100125
101- `appveyor/run_with_compiler .cmd <https://raw.githubusercontent.com/pypa/python-packaging-user-guide/master/source/code/run_with_compiler .cmd >`__
126+ `build .cmd <https://raw.githubusercontent.com/pypa/python-packaging-user-guide/master/source/code/build .cmd >`__
102127is a Windows batch script that runs a single command in an environment with the
103- appropriate compiler for the selected Python version.
128+ appropriate compiler for the selected Python version. All you need to do is to
129+ set the single environment variable ``DISTUTILS_USE_SDK `` to a value of ``1 ``
130+ and the script does the rest. It sets up the SDK needed for 64-bit builds of
131+ Python 3.3 or 3.4, so don't set the environment variable for any other builds.
104132
105- `appveyor/install.ps1 <https://raw.githubusercontent.com/pypa/python-packaging-user-guide/master/source/code/install.ps1 >`__ is a Powershell
106- script that downloads and installs any missing Python versions, installs
107- ``pip `` into the Python ``site-packages `` and downloads and installs the latest
108- ``wheel `` distribution. Steps that are not needed are omitted, so in practice,
109- the Python install will never be run (it is present for advanced users who want
110- to install additional versions of Python not supplied by Appveyor) and the
111- ``pip `` install will be omitted for Python 3.4, where pip is installed as
112- standard.
113-
114- You can simply download these two files and include them in your project
115- unchanged.
133+ You can simply download the batch file and include it in your project unchanged.
116134
117135
118136Access to the built wheels
@@ -128,6 +146,57 @@ wheels and upload them to PyPI as part of your release process.
128146Additional Notes
129147================
130148
149+ Testing with tox
150+ ----------------
151+
152+ Many projects use the `Tox `_ tool to run their tests. It ensures that tests
153+ are run in an isolated environment using the exact files that will be distributed
154+ by the project.
155+
156+ In order to use ``tox `` on Appveyor there are a couple of additional considerations
157+ (in actual fact, these issues are not specific to Appveyor, and may well affect
158+ other CI systems).
159+
160+ 1. By default, ``tox `` only passes a chosen subset of environment variables to the
161+ test processes. Because ``distutils `` uses environment variables to control the
162+ compiler, this "test isolation" feature will cause the tests to use the wrong
163+ compiler by default.
164+
165+ To force ``tox `` to pass the necessary environment variables to the subprocess,
166+ you need to set the ``tox `` configuration option ``passenv `` to list the additional
167+ environment variables to be passed to the subprocess. For the SDK compilers, you
168+ need
169+
170+ - ``DISTUTILS_USE_SDK ``
171+ - ``MSSdk ``
172+ - ``INCLUDE ``
173+ - ``LIB ``
174+
175+ The ``passenv `` option can be set in your ``tox.ini ``, or if you prefer to avoid
176+ adding Windows-specific settings to your general project files, it can be set by
177+ setting the ``TOX_TESTENV_PASSENV `` environment variable. The supplied ``build.cmd ``
178+ script does this by default whenever ``DISTUTILS_USE_SDK `` is set.
179+
180+ 2. When used interactively, ``tox `` allows you to run your tests against multiple
181+ environments (often, this means multiple Python versions). This feature is not as
182+ useful in a CI environment like Travis or Appveyor, where all tests are run in
183+ isolated environments for each configuration. As a result, projects often supply
184+ an argument ``-e ENVNAME `` to ``tox `` to specify which environment to use (there
185+ are default environments for most versions of Python).
186+
187+ However, this does *not * work well with a Windows CI system like Appveyor, where
188+ there are (for example) two installations of Python 3.4 (32-bit and 64-bit)
189+ available, but only one ``py34 `` environment in ``tox ``.
190+
191+ In order to run tests using ``tox ``, therefore, projects should probably use the
192+ default ``py `` environment in ``tox ``, which uses the Python interpreter that
193+ was used to run ``tox ``. This will ensure that when Appveyor runs the tests, they
194+ will be run with the configured interpreter.
195+
196+ In order to support running under the ``py `` environment, it is possible that
197+ projects with complex ``tox `` configurations might need to modify their ``tox.ini ``
198+ file. Doing so is, however, outside the scope of this document.
199+
131200Automatically uploading wheels
132201------------------------------
133202
@@ -145,32 +214,28 @@ External dependencies
145214---------------------
146215
147216The supplied scripts will successfully build any distribution that does not
148- rely on 3rd party external libraries for the build. It would be possible for an
149- individual project to add code to the ``install.ps1 `` script to make external
150- libraries available to the build, but this is of necessity specific to
151- individual projects.
217+ rely on 3rd party external libraries for the build.
218+
219+ It is possible to add steps to the ``appveyor.yml `` configuration (typically
220+ in the "install" section) to download and/or build external libraries needed by
221+ the distribution. And if needed, it is possible to add extra configuration for
222+ the build to supply the location of these libraries to the compiler. However,
223+ this level of configuration is beyond the scope of this document.
152224
153- Should projects develop scripts showing how to do this, references will be
154- added to this guide at a later date.
155225
156226Support scripts
157227---------------
158228
159- For reference, the two support scripts are listed here:
229+ For reference, the SDK setup support script is listed here:
160230
161- ``code/run_with_compiler .cmd ``
231+ ``code/build .cmd ``
162232
163- .. literalinclude :: code/run_with_compiler .cmd
233+ .. literalinclude :: code/build .cmd
164234 :language: bat
165235 :linenos:
166236
167- ``code/install.ps1 ``
168-
169- .. literalinclude :: code/install.ps1
170- :language: powershell
171- :linenos:
172-
173237.. _Appveyor : http://www.appveyor.com/
174238.. _Travis : https://travis-ci.org/
175239.. _Github : https://github.org/
176240.. _Bitbucket : https://bitbucket.org/
241+ .. _Tox : http://tox.testrun.org
0 commit comments