1Quick Start Guide
2-----------------
3
41. Install Microsoft Visual Studio 2017 with Python workload and
5 Python native development component.
61a. Optionally install Python 3.6 or later. If not installed,
7 get_externals.bat (via build.bat) will download and use Python via
8 NuGet.
92. Run "build.bat" to build Python in 32-bit Release configuration.
103. (Optional, but recommended) Run the test suite with "rt.bat -q".
11
12
13Building Python using Microsoft Visual C++
14------------------------------------------
15
16This directory is used to build CPython for Microsoft Windows on 32- and 64-
17bit platforms. Using this directory requires an installation of
18Microsoft Visual Studio (MSVC) with the *Python workload* and
19its optional *Python native development* component selected.
20
21Building from the command line is recommended in order to obtain any
22external dependencies. To build, simply run the "build.bat" script without
23any arguments. After this succeeds, you can open the "pcbuild.sln"
24solution in Visual Studio to continue development.
25
26To build an installer package, refer to the README in the Tools/msi folder.
27
28The solution currently supports two platforms. The Win32 platform is
29used to build standard x86-compatible 32-bit binaries, output into the
30win32 sub-directory. The x64 platform is used for building 64-bit AMD64
31(aka x86_64 or EM64T) binaries, output into the amd64 sub-directory.
32The Itanium (IA-64) platform is no longer supported.
33
34Four configuration options are supported by the solution:
35Debug
36 Used to build Python with extra debugging capabilities, equivalent
37 to using ./configure --with-pydebug on UNIX. All binaries built
38 using this configuration have "_d" added to their name:
39 python310_d.dll, python_d.exe, parser_d.pyd, and so on. Both the
40 build and rt (run test) batch files in this directory accept a -d
41 option for debug builds. If you are building Python to help with
42 development of CPython, you will most likely use this configuration.
43PGInstrument, PGUpdate
44 Used to build Python in Release configuration using PGO, which
45 requires Premium Edition of Visual Studio. See the "Profile
46 Guided Optimization" section below for more information. Build
47 output from each of these configurations lands in its own
48 sub-directory of this directory. The official Python releases may
49 be built using these configurations.
50Release
51 Used to build Python as it is meant to be used in production
52 settings, though without PGO.
53
54
55Building Python using the build.bat script
56----------------------------------------------
57
58In this directory you can find build.bat, a script designed to make
59building Python on Windows simpler. This script will use the env.bat
60script to detect either Visual Studio 2017 or 2015, either of
61which may be used to build Python. Currently Visual Studio 2017 is
62officially supported.
63
64By default, build.bat will build Python in Release configuration for
65the 32-bit Win32 platform. It accepts several arguments to change
66this behavior, try `build.bat -h` to learn more.
67
68
69C Runtime
70---------
71
72Visual Studio 2017 uses version 14.0 of the C runtime (vcruntime140).
73The executables no longer use the "Side by Side" assemblies used in
74previous versions of the compiler. This simplifies distribution of
75applications.
76
77The run time libraries are available under the redist folder of your
78Visual Studio distribution. For more info, see the Readme in the
79redist folder.
80
81
82Sub-Projects
83------------
84
85The CPython project is split up into several smaller sub-projects which
86are managed by the pcbuild.sln solution file. Each sub-project is
87represented by a .vcxproj and a .vcxproj.filters file starting with the
88name of the sub-project. These sub-projects fall into a few general
89categories:
90
91The following sub-projects represent the bare minimum required to build
92a functioning CPython interpreter. If nothing else builds but these,
93you'll have a very limited but usable python.exe:
94pythoncore
95 .dll and .lib
96python
97 .exe
98
99These sub-projects provide extra executables that are useful for running
100CPython in different ways:
101pythonw
102 pythonw.exe, a variant of python.exe that doesn't open a Command
103 Prompt window
104pylauncher
105 py.exe, the Python Launcher for Windows, see
106 https://docs.python.org/3/using/windows.html#launcher
107pywlauncher
108 pyw.exe, a variant of py.exe that doesn't open a Command Prompt
109 window
110_testembed
111 _testembed.exe, a small program that embeds Python for testing
112 purposes, used by test_capi.py
113
114These are miscellaneous sub-projects that don't really fit the other
115categories:
116_freeze_module
117 _freeze_module.exe, used to regenerate frozen modules in Python
118 after changes have been made to the corresponding source files
119 (e.g. Lib\importlib\_bootstrap.py).
120pyshellext
121 pyshellext.dll, the shell extension deployed with the launcher
122python3dll
123 python3.dll, the PEP 384 Stable ABI dll
124xxlimited
125 builds an example module that makes use of the PEP 384 Stable ABI,
126 see Modules\xxlimited.c
127xxlimited_35
128 ditto for testing the Python 3.5 stable ABI, see
129 Modules\xxlimited_35.c
130
131The following sub-projects are for individual modules of the standard
132library which are implemented in C; each one builds a DLL (renamed to
133.pyd) of the same name as the project:
134_asyncio
135_ctypes
136_ctypes_test
137_zoneinfo
138_decimal
139_elementtree
140_hashlib
141_msi
142_multiprocessing
143_overlapped
144_socket
145_testbuffer
146_testcapi
147_testconsole
148_testimportmultiple
149_testmultiphase
150_tkinter
151pyexpat
152select
153unicodedata
154winsound
155
156The following Python-controlled sub-projects wrap external projects.
157Note that these external libraries are not necessary for a working
158interpreter, but they do implement several major features. See the
159"Getting External Sources" section below for additional information
160about getting the source for building these libraries. The sub-projects
161are:
162_bz2
163 Python wrapper for version 1.0.8 of the libbzip2 compression library
164 Homepage:
165 http://www.bzip.org/
166_lzma
167 Python wrapper for version 5.2.2 of the liblzma compression library
168 Homepage:
169 https://tukaani.org/xz/
170_ssl
171 Python wrapper for version 1.1.1u of the OpenSSL secure sockets
172 library, which is downloaded from our binaries repository at
173 https://github.com/python/cpython-bin-deps.
174
175 Homepage:
176 https://www.openssl.org/
177
178 Building OpenSSL requires Perl on your path, and can be performed by
179 running PCbuild\prepare_ssl.bat. This will retrieve the version of
180 the sources matched to the current commit from the OpenSSL branch
181 in our source repository at
182 https://github.com/python/cpython-source-deps.
183
184 To use an alternative build of OpenSSL completely, you should replace
185 the files in the externals/openssl-bin-<version> folder with your own.
186 As long as this folder exists, its contents will not be downloaded
187 again when building.
188
189_sqlite3
190 Wraps SQLite 3.42.0, which is itself built by sqlite3.vcxproj
191 Homepage:
192 https://www.sqlite.org/
193_tkinter
194 Wraps version 8.6.6 of the Tk windowing system, which is downloaded
195 from our binaries repository at
196 https://github.com/python/cpython-bin-deps.
197
198 Homepage:
199 https://www.tcl.tk/
200
201 Building Tcl and Tk can be performed by running
202 PCbuild\prepare_tcltk.bat. This will retrieve the version of the
203 sources matched to the current commit from the Tcl and Tk branches
204 in our source repository at
205 https://github.com/python/cpython-source-deps.
206
207 The two projects install their respective components in a
208 directory alongside the source directories called "tcltk" on
209 Win32 and "tcltk64" on x64. They also copy the Tcl and Tk DLLs
210 into the current output directory, which should ensure that Tkinter
211 is able to load Tcl/Tk without having to change your PATH.
212
213
214Getting External Sources
215------------------------
216
217The last category of sub-projects listed above wrap external projects
218Python doesn't control, and as such a little more work is required in
219order to download the relevant source files for each project before they
220can be built. However, a simple script is provided to make this as
221painless as possible, called "get_externals.bat" and located in this
222directory. This script extracts all the external sub-projects from
223 https://github.com/python/cpython-source-deps
224and
225 https://github.com/python/cpython-bin-deps
226via a Python script called "get_external.py", located in this directory.
227If Python 3.6 or later is not available via the "py.exe" launcher, the
228path or command to use for Python can be provided in the PYTHON_FOR_BUILD
229environment variable, or get_externals.bat will download the latest
230version of NuGet and use it to download the latest "pythonx86" package
231for use with get_external.py. Everything downloaded by these scripts is
232stored in ..\externals (relative to this directory).
233
234It is also possible to download sources from each project's homepage,
235though you may have to change folder names or pass the names to MSBuild
236as the values of certain properties in order for the build solution to
237find them. This is an advanced topic and not necessarily fully
238supported.
239
240The get_externals.bat script is called automatically by build.bat
241unless you pass the '-E' option.
242
243
244Profile Guided Optimization
245---------------------------
246
247The solution has two configurations for PGO. The PGInstrument
248configuration must be built first. The PGInstrument binaries are linked
249against a profiling library and contain extra debug information. The
250PGUpdate configuration takes the profiling data and generates optimized
251binaries.
252
253The build_pgo.bat script automates the creation of optimized binaries.
254It creates the PGI files, runs the unit test suite or PyBench with the
255PGI python, and finally creates the optimized files.
256
257See
258 https://docs.microsoft.com/en-us/cpp/build/profile-guided-optimizations
259for more on this topic.
260
261
262Static library
263--------------
264
265The solution has no configuration for static libraries. However it is
266easy to build a static library instead of a DLL. You simply have to set
267the "Configuration Type" to "Static Library (.lib)" and alter the
268preprocessor macro "Py_ENABLE_SHARED" to "Py_NO_ENABLE_SHARED". You may
269also have to change the "Runtime Library" from "Multi-threaded DLL
270(/MD)" to "Multi-threaded (/MT)".
271
272
273Visual Studio properties
274------------------------
275
276The PCbuild solution makes use of Visual Studio property files (*.props)
277to simplify each project. The properties can be viewed in the Property
278Manager (View -> Other Windows -> Property Manager) but should be
279carefully modified by hand.
280
281The property files used are:
282 * python (versions, directories and build names)
283 * pyproject (base settings for all projects)
284 * openssl (used by projects dependent upon OpenSSL)
285 * tcltk (used by _tkinter, tcl, tk and tix projects)
286
287The pyproject property file defines all of the build settings for each
288project, with some projects overriding certain specific values. The GUI
289doesn't always reflect the correct settings and may confuse the user
290with false information, especially for settings that automatically adapt
291for different configurations.
292