Python Forum
[PyGame] Install and Troubleshooting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Install and Troubleshooting
#1
Before you read all this you should know that pygame should work from simply doing
pip install pygame
On linux systems to get python3.x you will need to do:
pip3 install pygame
pip should find the correct pygame for whatever python version that pip is under, download, and install it and that is that.
The following is only if this does not work or other complications. you should also read the official wiki from pygame. At the end of this post is a troubleshooting section with common issues people have with getting pygame to work.

Install the correct bit type
Make sure you install the correct pygame version for your python version. If your python version is 64 bit, and you install pygame 32 bit, there is going to be a problem. Python and Pygame need to be the same bittype. However Python does not need to be what bit type our OS is. You can have a 64 Windows, and have a 32 bit python installed. That would just mean you need a pygame 32 as well. The bit type is labeled in the name of the wheel. win32, i686, x86, x86-32 is 32 bit, while x86_64, x64, amd64 is 64 bit. A reliable way to determine your python bit type is to invoke the python version that you plan on installing pygame to with these arguments in your terminal/command prompt. The result will be 64 or 32.
python -c 'import struct;print( 8 * struct.calcsize("P"))'
Install the correct Pygame version for your Python version
Make sure you install the correct pygame version for your intended python version to use with pygame. If you have multiple python versions installed, make note to which one you installed to and use that interpreter when running pygame. This may require you to modify which interpreter is used in IDE's. You can bypass the IDE and use a terminal/command prompt to test if successful. The IDE is just a config issue. The associated python version for that pygame will be on the name of the wheel. cp27 or py27 is python 2.7 while cp36 or py36 is python 3.6, and so on and so on.

Often times when a new python release comes out or a new pygame release comes out it is very much possible that it is incompatible with other versions. For example there has been a history in Pygame's time when the newest Python version was not supported for the latest Pygame version. This meant that you must install an older Python. This is completely depending on how many people are working on Pygame at any given time. But you should be aware of this possibility.

Install the correct OS type Pygame
If you have Windows, you cannot install a pygame that is intended for Linux, and vice versa. Make sure you install the pygame version for your operating system. The OS type is in the name of the wheel. win32 or win_amd64 is Windows 32 bit and 64 bit, while manylinux or linux is Linux, and macosx is Macintosh/Apple.

If you mismatch any of these when installing you are going to have problems. Sometimes you actually get a "not a supported wheel for this platform" or "no matching architecture..." error that explicitly states the problem.....but sometimes it lets you install the incorrect one. Makes sure you install the correct one. If you have an issue and think you installed the wrong one, uninstall it, and reinstall the correct one and test again. This is 9 times out of 10 the reason why people post on forums saying their pygame is not working is that they installed the wrong wheel.

pip uninstall pygame
Download the right pygame from the right place
Windows binaries are from the unofficial site, but they are constantly updated and have always been.
All others would have to get them from Pypi, pygame download page (which can be outdated) or from their bitbucket or new Github location.

Most likely you will ever need to go to Pypi to get the pygame wheel. That is the most legit method. However if your on Windows, you can also use the link proivided above. Sometimes they are actually more updated than Pypi. But that is only for windows.

Install wheel with pip
You can install a wheel with the pip command. This comes with python since Python 2.7.9+ and Python 3.4+. IF you have an older installation you will need to install pip then. Change <FILE.whl> to the path and name of the wheel for your python version that you downloaded. Navigate to your python directory and execute the following command.
python.exe -m pip install <FILE.whl>
where python.exe is you python executable intended to have pygame installed. You can also navigate into the SCripts folder where pip is and execute
pip.exe install <FILE.whl>

The <File.whl> is the full path to where ever tyou downloaded the wheel to. So it could be C:\Users\USERNAME\Downloads\filename.whl in which case you would do
pip.exe install C:\Users\USERNAME\Downloads\filename.whl
For linux/mac users you can omit the .exe

Lets assume that you did a custom installtion and installed python at the location of C:\PythonXX. This is the default location for python3.4 and lower, but since python3.5 the default location has been at C:\Users\USERNAME\AppData\Local\Programs\Python\PythonXX-XX However to even access your AppData directory on Windows, you have to change settings to show hidden directories and files in control panel -> File Explorer Options -> View -> Hidden files and folders -> check yes and then apply changes. You can bypass this just by changing the default install location of python within the install wizard by choosing custom install and typing a new install path. C:\Python35-32 for example.

c:\Python35-32\Scripts>
c:\Python35-32\Scripts>pip install C:\Users\YOUR_USERNAME\Downloads\pygame-1.9.2a0-cp35-none-win32.whl
from the directory:
C:\Python35-32\
execute:
python.exe
to open the interpreter for testing and when you get a >>> prompt put in and hit enter
import pygame
If you get a >>> prompt with no errors then you installed pygame correctly and it is working. For more verification you can open a pygame window as shown here to make sure everything is fully working.

pip may or may not give a warning saying there is a newer pip you can install. However this is just a warning. Pygame should of installed correctly even with an old pip. Open a python interpreter and test if pygame can be imported without an error.

Pip is just a PyPi package like any other, you can use it to upgrade itself the way you would any other package.

pip install --upgrade pip
Install from source
Sometimes you might want to bypass a bug by installing from source, or get the latest possible pygame, try out the dev version, etc. To do this you need a compiler. Windows does not come with one by default, while linux/mac do.

You need to make sure you have installed dependencies. For ubuntu linux this would entitle the following.
sudo apt-get install python3-dev python-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev python3-numpy python-numpy subversion libportmidi-dev libfreetype6-dev
For Mac it would be
brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi
For windows, see here
If you want to compile SDL itself it can be obtained from their website here.

Now we need to download pygame. If you have git you can just clone the repo
git clone https://github.com/pygame/pygame.git
If you dont have git, you can install git or you can just open this link and click download zip, unzip and you have the same thing.

You can also download the source from pypi. This is the latest stable version. If you want dev/latest, get it from github.

From there there will be a pygame directory. Change directory to there and execute
python setup.py install 
assuming you installed the dependencies, you should have no error. Make sure to runs some test windows, etc. If you have issues please make a post so we can help you fix it.

If you have an issues with installing pygame or getting it to run properly, please feel free to post on the forums. Please give as much information as possible. However check the troubleshooting pygame issues below before doing so, your issue might just be there already solved.

Testing to see if Pygame works
You can run in the terminal/command prompt python with these arguments to check each pygame dependency.
python -m pygame.tests
where python is whatever version you just installed pygame to.

Points of Interest
Pygame requires SDL1.2+. Pygame doesn't currently work with SDL2. However it is being developed at the time of this writing.
Pygame_SDL2 is a reimplementation of the Pygame API using SDL2 and related libraries.

Troubleshooting
You can have problems anywhere from importing pygame, causing an import error to the window opening, then closing unexpectedly. Or you can have a sigfault somewhere midgame due to a problem with SDL_Image or something. You may have issues where your program lags or your CPU maxs at 100%. Each error requires different fix.

Make sure to check the open and closed issues at the pygame repo. You might find out that your problem has already been fixed. If not you can make an issue there and the authors of pygame will respond accordingly. Might as well go straight to the source if it is a true bug issue.

pygame just hasnt been updated yet
Sometimes getting the latest recent python release means libraries have not yet updated to that version. This can cause an unlimited number of odd errors (too many to list). Try installing an earlier version of python and the associated pygame version for that python to see if it is the case or not. The unoffical binaries linked above to download pygame section usually has the most recent pygame patched to fix until the official release is out. But because it unoffical, if it does not work, there is not much to do other than to wait until there is an offical one. This is more of a concern with a minor python updates rather than a patch one, but that is not always the case.

mixing pygame and other GUI frameworks
Its well known that mixing pygame with other frameworks like Tkinter are going to result in a lot of odd hard-to-diagnose problems. Its best to not mix them at all. Use pygame built libraries or build them yourself, unfortunately pygame does not have default user interface, etc.

Do a bare bones test
When your troubleshooting, strip away everything but python and pygame. This means dont use an IDE for testing if it works because the IDE might not be configured correctly or might have issues. The best way to test if pygame is working is by opening up a terminal/command prompt and executing python. And only from there import pygame. This weeds out any issue with the IDE. If that works then you know its your IDE, if not then there is still something wrong.
Also strip any other imported libraries you are using from the test. Make sure it is a problem with pygame in the first place.

Access is denied / PermissionError
You need admin privileges (Windows) or sudo powers (Linux) to install.


ImportError: No module named base
Error:
from pygame.base import * ImportError: No module named base
Either you have an incorrect pygame wheel in relation to your python target, or that python version is not yet supported by pygame.

Pygame/Python is slow or maxing my CPU%/RAM
Most of the time the issue arises with it being slow or maxing your CPU out is because you yourself created a bottleneck in your code. This can happen in other programming languages, even c++. First make sure bottlenecks are not in your code before blaming python/pygame. Do some minor code enhancements to speed up sections that are slow.
  • Do not load resources within the main game loop. This can easily lag your program. Resources are define as any disk loading (images, sound, music, etc.)
  • Always load your images using .convert() or .convert_alpha(). Otherwise it will do every frame
  • Reading and writing to disc is the slowest part. Either minimize this, or enhance the parts that are truly required.
  • Use existing, well known 3rd party libraries instead of making your own reproductions of them. These libraries are updated and have been tweaked for maximum speed already, so why try to recreate it? One case might be NumPy usage.
  • pygame.draw to a surface and blit the surface. Otherwise numerous pygame.draw calculations may slow down the process.
  • You can handle more images with sprite classes and group classes

sound is delayed or causes crash
This is the common fix. You need to add this line before you call pygame.init()
pygame.mixer.pre_init(44100, -16, 1, 512)
Problems with built exe's

A common occurance of a failing exe is something like:
font = pygame.font.SysFont(None, 100)
you can either not use a system font, add one to your package, and use that or you can try a common font such as:
font = pygame.font.SysFont('Sans', 100)
You can verify a system font if its in this list (not all systems have the same fonts). A safe approach is to you your own font
sorted(pygame.font.get_fonts())
As for any other problems when building an exe, you can have a lot of weird issues when doing so. Post on the forums your issue, and when we get it resolved, we will add that to this list.

You can also use a GUI method of using pyinstaller to build your exes whihc might help if you are unfamiliar with command prompt/terminal or with pyinstaller.
https://github.com/brentvollebregt/auto-py-to-exe

there is also a list of things to check when things go wrong in pyinstaller.

Macs
A note to Mac users....Mac users have more problems than Linux and Windows users with Pygame. Ive seen where an issue was fixed by downgrading sdl_image because of a bug for mac to the user installed the incorrect wheel. If you have an issue that cannot be resolved then you can seek help by the authors of pygame by creating an issue here

You can find more info here

There is a graphics bug under El Capitan that requires downgrading a particular library (sdl_image). sdl_image 1.2.12 needs to be downgraded to sdl_image 1.2.10. More info on that here

Unfortunately mac users have to jump through more hoops than others regarding pygame. I do not have a mac, so i would love to hear fixes to list out here. Post elsewhere and link back here and I will modify it accordingly.

suppress help link from pygame
The upcoming release 1.9.5 of pygame will include an option to turn off the message without modifying the library:
You have to set the environment variable PYGAME_HIDE_SUPPORT_PROMPT to any value
On Windows: set PYGAME_HIDE_SUPPORT_PROMPT=1
On Linux etc.: export PYGAME_HIDE_SUPPORT_PROMPT=1

Or even in your code:
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

import pygame  # it is important to import pygame after that
Note, that modifying the library would mean that you would have to modify the library everywhere you ship your code.

Pygame dependencies
Sometimes you may be missing dependencies for pygame. This is not installed with pip. Often this is not a Windows issue (unless you compiled it), but linux/mac. These are installed with your package manager into your system first before trying to install pygame with pip.
src/pygame.h:75:20: fatal error: Python.h: No such file or directory compilation terminated. error: command 'i686-linux-gnu-gcc' failed with exit status 1
This specifically means you do not have python3.dev package for your linux/mac system.

When your installing pygame make sure this list has everything as "found" as you want. It is possible to compile pygame without a feature you want.
Hunting dependencies...
SDL     : found 1.2.14
FONT    : found
IMAGE   : found
MIXER   : found
SMPEG   : found 0.4.5
PNG     : found
JPEG    : found
SCRAP   : found
PORTMIDI: found
PORTTIME: found
AVFORMAT: not found
SWSCALE : not found
FREETYPE: found 2.4.8
 
 
Warning, some of the pygame dependencies were not found. Pygame can still
compile and install, but games that depend on those missing dependencies
will not run. Would you like to continue the configuration? [Y/n]:
More information
Running these commands in a python interpreter after you import pygame will give you a lot of information about pygame that may be required to fix some issues.
print(pygame.init()) Show how many modules were successful and failed.
print(pygame.get_error()) See if and sdl errors happen.
print(pygame.__file__) To see where it's importing from.

To be continued...
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020