Skip to main content

CompileWindows — wiki

Compile pygame on windows

There are three steps.

  • Get a C/C++ compiler.
  • Checkout pygame from github.
  • Run the pygame install commands.

Step 1, Get a C/C++ compiler.

  • Python 3.6+ Install Microsoft Build Tools for Visual Studio 2017.

    The setuptools Python package version must be at least 34.4.0.

    py -3 -m pip install setuptools -U
    
  • Make sure this checkbox is ticked: "[ ] MSVC v140 - VS 2015 C++ build tools (v14.00)"

These will download the required dependencies and build for SDL2(experimental) or SDL1.

Step 2, Checkout pygame from github.

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

Step 3, Run the pygame install commands.

SDL2 instructions.

git clone https://github.com/pygame/pygame.git
cd pygame
py -3 -m pip install setuptools requests wheel numpy -U
py -3 -m buildconfig --download
py -3 pip install .
py -3 -m pygame.examples.aliens

More information

Compiler information pages for windows.

old instructions

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.

Compilation Logs/Attempts

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.

Attempt to compile pygame on Windows - 21/04/2020 - Python 3.8.2, 32 bit

Following these steps:

  1. Download Visual Studio Build Tools 2017 & Windows 10 SDK from here
  2. Then try the following 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
    
  3. For me, this produced a
    distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat
    
    error on step:
    python -m buildconfig -sdl2 --download
    
  4. From there I edited part of 'buildconfig/config_win.py' on line #454 to read:
    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.
  5. After that I got one error on
    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.
  6. At the end I could run some pygame projects so long as they didn't use the imageext module which seemed not to work.



Attempt to compile pygame on Windows - 02/09/2020 - Python 3.8.5, 32 bit



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):

  • MSVC v142 - VS2019 C++ x64/x86 build tools (v 14.27)
  • Windows 10 SDK (10.0.18362.0)
  • C++ CMake tools for windows
  • Testing tools core features - Build Tools
  • C++ AddressSanitizer (Experimental)

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\<username_here>\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.