There are four steps.
Install Microsoft Build Tools for Visual Studio 2017.
If the link doesn't work, here are the links to download the Visual Studio Build Tools from Microsoft directly.
2017 2019 2022The setuptools Python package version must be at least 34.4.0.py -m pip install setuptools -U
These will download the required dependencies and build for SDL2.
To get pygame from github, you might need to install git. This is a good command line option for git checkouts on windows. https://gitforwindows.org/
Here is the pygame github repo where the code lives. https://github.com/pygame/pygame
set DISTUTILS_USE_SDK=1
set MSSdk=1
git clone https://github.com/pygame/pygame.git
cd pygame
py -m pip install setuptools requests wheel numpy -U
py -m buildconfig --download
py -m pip install .
py -m pygame.examples.aliens
Compiler information pages for windows.
There are here for historical interest.
The incomplete guide with mingw is here: Compiling with MingW gcc on windows.
The incomplete guide with the Visual Studio 2003 Toolkit compiler is here: Compiling with the Toolkit Compiler.
These may be useful for people encountering issues attempting to compile on windows as depending on what version of python and Visual Studio you use, things can get more interesting. The most successful attempt so far with python 3.8 is at the bottom.
Following these steps:
git clone https://github.com/pygame/pygame.git
cd pygame
python -m pip install setuptools requests -U
python -m buildconfig -sdl2 --download
python setup.py install
python -m pygame.examples.aliens
distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat
error on step: python -m buildconfig -sdl2 --download
find_lib=r'freetype[-0-9]*\.lib'
And line #463 to read find_lib=r'(lib)?png[-0-9]*\.lib'
And line #468 to read find_lib=r'(lib)?jpeg\.lib'
Which let the process compile past that step.python setup.py install
Reading: prebuilt-x86/include\ft2build.h(56): fatal error C1083: Cannot open include file: 'freetype/config/ftheader.h': No such file or directory
To fix this I moved the 'freetype' folder from 'pygame\prebuilt-x86\include\freetype2' to 'pygame\prebuilt-x86\include\' and it then seemed to compile all the way to the end.This attempt was successful (imageext module fully working) but I encountered a few issues along the way.
Visual Studio build tools installed (don't know if all of these are crucial but they were the defaults for me):
Once those were downloaded and installed I loaded up the 'Developer Command Prompt for VS 2019' to try a compile. Make sure that you pick correct prompt 32 or 64 bit prompt. Following the instructions above, but using one additional step at the start:
set DISTUTILS_USE_SDK=1
set MSSdk=1
git clone https://github.com/pygame/pygame.git
cd pygame
python -m pip install setuptools requests -U
python -m buildconfig -sdl2 --download
2020-10-07 / jtiai
Documentation of setuptools/distutils do say that both DISTUTILS_USE_SDK and MSSdk environment variables must be defined. After that config and building should work without issues. I didn't need to do steps below to modify any files.
At first this didn't work for me, failing with an error because disutils was failing to find the install location of the Visual Studio Build Tools. I'm not sure if there is a better way to correct this but I was able to make two small changes to msvc.py which is located at:
C:\Users\\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\setuptools\
The first change was adding the literal path to the build tools into the code on line 862. The block of code now looks like this:
try:
# First search in known VS paths
vs_dir = self.known_vs_paths[self.vs_ver]
except KeyError:
# Else, search with path from registry
vs_dir = r'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools' # Edited this line
The second change was to change the index into a sub-directory where it was clearly looking into the wrong one, this is around line 1709 and now looks like this:
# Installation prefixes candidates
prefixes = []
tools_path = self.si.VCInstallDir
redist_path = dirname(tools_path.replace(r'\Tools', r'\Redist'))
if isdir(redist_path):
# Redist version may not be exactly the same as tools
redist_path = join(redist_path, listdir(redist_path)[0]) # Edit this line
prefixes += [redist_path, join(redist_path, 'onecore')]
with the original -1 (picking the last sub-directory in the list) changed to a 0 (picking the first).
With those two changes I was able to run (note from jtiai 2020-10-07: using below both 32 and 64 bit compilation works and no changes in files were required:
set DISTUTILS_USE_SDK=1
set MSSdk=1
python setup.py clean --all
python -m buildconfig -sdl2 --download
python setup.py install
python -m pygame.examples.aliens
And this time it all compiled successfully. I also tried the same process but with x64 bit python - but it failed and despite some tweaking, I wasn't able to resolve it and get 64 bit pygame compiled on windows.
Update - Report from jtai on Discord that setting:
MSSdk=1
Is required to make the 64bit build work.