In this article, we will learn how to Install PyGame module of Python on Windows. PyGame is a library of python language. It is used to develop 2-D games and is a platform where you can set python modules to develop a game. It is a user-friendly platform that helps to build games quickly and easily.
Follow the steps given below for the successful installation of Pygame
Step 1: Check for Python Installation
In order to install Pygame, Python must be installed already in your system. To check whether Python is installed or not in your system, open the command prompt and give the command as shown below.
If this command runs successfully, and we are able to get a Python version then we are good to go. Otherwise, we have to install Python in our system, to do this refer to How to install Python on Windows?
Step 2: Check for PIP installation
PIP is a tool that is used to install python packages. PIP is automatically installed with Python 2.7. 9+ and Python 3.4+. Open the command prompt and enter the command shown below to check whether pip is installed or not.
Note: Refer to How to install PIP on Windows ? for detailed information.
Step 3: Install Pygame
To install Pygame, open the command prompt and give the command as shown below:
pip install pygame
Pygame is successfully installed as shown in the image above.
Step 4: Check Whether PyGame is Working or not
Now open a new terminal and import the Pygame library to see whether it is working fine or not in our system. The library is imported successfully means we got success.
In this way, we can install the pygame module in Python.
Last Updated :
21 Feb, 2023
Like Article
Save Article
Pygame is a free and open-source cross-platform library
for the development of multimedia applications like video games using Python.
It uses the Simple DirectMedia Layer library and several other
popular libraries to abstract the most common functions, making writing
these programs a more intuitive task.
We need your help to make pygame the best it can be!
New contributors are welcome.
Installation
Before installing pygame, you must check that Python is installed
on your machine. To find out, open a command prompt (if you have
Windows) or a terminal (if you have MacOS or Linux) and type this:
python --version
If a message such as “Python 3.8.10” appears, it means that Python
is correctly installed. If an error message appears, it means that
it is not installed yet. You must then go to the official website and follow the instructions.
Once Python is installed, you have to perform a final check: you have
to see if pip is installed. Generally, pip is pre-installed with
Python but we are never sure. Same as for Python, type the following
command:
pip --version
If a message such as “pip 20.0.2 from /usr/lib/python3/dist-packages/pip
(python 3.8)” appears, you are ready to install pygame! To install
it, enter this command:
pip install pygame
Help
If you are just getting started with pygame, you should be able to
get started fairly quickly. Pygame comes with many tutorials and
introductions. There is also full reference documentation for the
entire library. Browse the documentation on the docs page. You
can also browse the documentation locally by running
python -m pygame.docs in your terminal. If the docs aren’t found
locally, it’ll launch the online website instead.
The online documentation stays up to date with the development version
of pygame on github. This may be a bit newer than the version of pygame
you are using. To upgrade to the latest full release, run
pip install pygame —upgrade in your terminal.
Best of all, the examples directory has many playable small programs
which can get you started playing with the code right away.
Pygame is a powerful library for game development, offering a wide
range of features to simplify your coding journey. Let’s delve into
what Pygame has to offer:
Graphics: With Pygame, creating dynamic and engaging graphics has
never been easier. The library provides simple yet effective tools for
2D graphics and animation, including support for images, rectangles,
and polygon shapes. Whether you’re a seasoned game developer or just
starting out, Pygame has you covered.
Sound: Pygame also includes support for playing and manipulating sound
and music, making it easy to add sound effects and background music to
your games. With support for WAV, MP3, and OGG file formats, you have
plenty of options to choose from.
Input: Pygame provides intuitive functions for handling keyboard, mouse,
and joystick input, allowing you to quickly and easily implement player
controls in your games. No more struggling with complex input code, Pygame
makes it simple.
Game Development: Lastly, Pygame provides a comprehensive suite of tools
and features specifically designed for game development. From collision
detection to sprite management, Pygame has everything you need to create
exciting and engaging games. Whether you’re building a platformer, puzzle
game, or anything in between, Pygame has you covered.
Building From Source
If you want to use features that are currently in development,
or you want to contribute to pygame, you will need to build pygame
locally from its source code, rather than pip installing it.
Installing from source is fairly automated. The most work will
involve compiling and installing all the pygame dependencies. Once
that is done, run the setup.py script which will attempt to
auto-configure, build, and install pygame.
Much more information about installing and compiling is available
on the Compilation wiki page.
Contribute
Thank you for thinking of contributing!
To contribute to the main project documentation, see docs/README.md or view more detailed instructions here.
New to contributing to Open Source Free Libre software?
There is a draft of “Let’s write a unit test!” which is a step by step guide on how to write your first unit test in python for pygame, which is very similar to how you would do it for other projects.
Want or need to compile pygame from source?
For a detailed developer guide on “How to Hack Pygame”:
To submit patches and report bugs:
See the info page for more info and ways to get in touch with the Pygame team.
Credits
Thanks to everyone who has helped contribute to this library.
Special thanks are also in order.
-
Marcus Von Appen: many changes, and fixes, 1.7.1+ freebsd maintainer
-
Lenard Lindstrom: the 1.8+ windows maintainer, many changes, and fixes
-
Brian Fisher for svn auto builder, bug tracker and many contributions
-
Rene Dudfield: many changes, and fixes, 1.7+ release manager/maintainer
-
Phil Hassey for his work on the pygame.org website
-
DR0ID for his work on the sprite module
-
Richard Goedeken for his smoothscale function
-
Ulf Ekström for his pixel perfect collision detection code
-
Pete Shinners: original author
-
David Clark for filling the right-hand-man position
-
Ed Boraas and Francis Irving: Debian packages
-
Maxim Sobolev: FreeBSD packaging
-
Bob Ippolito: MacOS and OS X porting (much work!)
-
Jan Ekhol, Ray Kelm, and Peter Nicolai: putting up with early design ideas
-
Nat Pryce for starting our unit tests
-
Dan Richter for documentation work
-
TheCorruptor for his incredible logos and graphics
-
Nicholas Dudfield: many test improvements
-
Alex Folkner for pygame-ctypes
Thanks to those sending in patches and fixes: Niki Spahiev, Gordon
Tyler, Nathaniel Pryce, Dave Wallace, John Popplewell, Michael Urman,
Andrew Straw, Michael Hudson, Ole Martin Bjoerndalen, Herve Cauwelier,
James Mazer, Lalo Martins, Timothy Stranex, Chad Lester, Matthias
Spiller, Bo Jangeborg, Dmitry Borisov, Campbell Barton, Diego Essaya,
Eyal Lotem, Regis Desgroppes, Emmanuel Hainry, Randy Kaelber
Matthew L Daniel, Nirav Patel, Forrest Voight, Charlie Nolan,
Frankie Robertson, John Krukoff, Lorenz Quack, Nick Irvine,
Michael George, Saul Spatz, Thomas Ibbotson, Tom Rothamel, Evan Kroske,
Cambell Barton.
And our bug hunters above and beyond: Angus, Guillaume Proux, Frank
Raiser, Austin Henry, Kaweh Kazemi, Arturo Aldama, Mike Mulcheck,
Michael Benfield, David Lau
There’s many more folks out there who’ve submitted helpful ideas, kept
this project going, and basically made our life easier. Thanks!
Many thank you’s for people making documentation comments, and adding to the
pygame.org wiki.
Also many thanks for people creating games and putting them on the
pygame.org website for others to learn from and enjoy.
Lots of thanks to James Paige for hosting the pygame bugzilla.
Also a big thanks to Roger Dingledine and the crew at SEUL.ORG for our
excellent hosting.
Dependencies
Pygame is obviously strongly dependent on SDL and Python. It also
links to and embeds several other smaller libraries. The font
module relies on SDL_ttf, which is dependent on freetype. The mixer
(and mixer.music) modules depend on SDL_mixer. The image module
depends on SDL_image, which also can use libjpeg and libpng. The
transform module has an embedded version of SDL_rotozoom for its
own rotozoom function. The surfarray module requires the Python
NumPy package for its multidimensional numeric arrays.
Dependency versions:
CPython |
>= 3.6 (Or use PyPy3) |
SDL |
>= 2.0.8 |
SDL_mixer |
>= 2.0.0 |
SDL_image |
>= 2.0.2 |
SDL_ttf |
>= 2.0.11 |
SDL_gfx |
(Optional, vendored in) |
NumPy |
>= 1.6.2 (Optional) |
License
This library is distributed under GNU LGPL version 2.1, which can
be found in the file docs/LGPL.txt. We reserve the right to place
future versions of this library under a different license.
This basically means you can use pygame in any project you want,
but if you make any changes or additions to pygame itself, those
must be released with a compatible license (preferably submitted
back to the pygame project). Closed source and commercial games are fine.
The programs in the examples subdirectory are in the public domain.
See docs/licenses for licenses of dependencies.
Example
On windows
-
Navigate to http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame – an unofficial site providing windows binaries of open-source python packages for the official CPython distribution by Christoph Gohlke.
-
Download the appropriate pygame
.whl
file according to your installed python version. (The file is named something likepygame -
<pygame version> - <python version>
- win32.whl
) -
Run
pip install your-pygame-package.whl
inside your terminal, bash or consol.
Note: ifpip
is not found inPATH
try to runpython -m pip install your-pygame-package.whl
-
Check if you can import pygame as a python module
import pygame
If you do not get an error, you have correctly installed pygame on your computer
On linux
-
Open your terminal and run
sudo apt-get install python-pygame
Note: This will install pygame for python2
-
Try to import pygame inside
import pygame
If you do not get an error, you have correctly installed pygame on your linux system
On macOS
There are two ways to install it on mac:
Method 1
Go to the Pygame downloads page and download the mac installer. Run it, and it should install Pygame on your Mac.
Method 2
Install Homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then use Homebrew to install Python 2.7.12 and Pygame:
brew install python; brew install homebrew/python/pygame
Now run Python in your terminal and try import pygame
. If it does not say anything, it’s installed successfully.
Edit me
Pygame is a set of Python modules used for writing simple video games and graphical programs. It is cross-platform, which means it can be installed on both Windows and Mac computers. This guide provides the steps needed to install Pygame for Python 3.6 on Windows and macOS.
Procedure (Windows 10)
- Make sure to have the latest version of Python 3 installed on your computer. If you need to install Python 3, you can visit (this site) to install the latest Python 3 release.
-
When running the Python installer, you will have to select Customize installation. Click Next and check the box next to Install for all users at the top of the list in Advanced Options. You can now click Install.
- Once Python 3 is installed, launch the File Explorer.
-
Click This PC, select the drive where Windows is installed (usually the C:\ drive), double-click on the Program Files (x86) folder, and double-click on the Python folder (i.e. Python36-32).
-
Click on the path in the text box towards the top of the File Explorer window and copy it to the clipboard.
- Next, you will need to launch the Control Panel. To do this, open the Start Menu and search control in the search box. The Control Panel should be the first option that appears in the Start Menu.
- Click on System and Security, then System, then Advanced System Settings.
-
In the window that appears, click on Enviroment Variables…
-
You will need to edit the Path system variable. To do this, select Path in the System variables section of the window and click Edit…
- Click New and paste the path copied from earlier into the empty textbox that is generated. Click OK twice to save the changes.
- You will now need to open the Command Prompt as an administrator. To do this, open the start menu and search cmd in the search box. The Command Prompt should be the first option that appears in the Start Menu. Right click on that option and choose Run as administrator.
- In the Command Prompt, type cd and a space and right-click in the Command Prompt to paste the path to Python. Press Enter.
- Type this command into the Command Prompt and press Enter:
- You can now install Pygame. To do this, type the following command and press Enter:
- Pygame should have successfully installed. To verify this, type the following command to launch the Python interactive interpreter:
- Type the following line in the interpreter and press Enter:
If entering the previous line into the interpreter does not throw any errors, you have successfully installed Pygame on Windows. You can now write any graphical program you want using the Pygame library.
Procedure (Mac OS X)
This guide is based on an article found (here)
- Before installing Pygame, there are many dependencies that will need to be installed. Begin by installing the XCode developer tools, which can be done through the app store.
- Next you will need to install the latest version of XQuartz. You can start the installation by visiting the site (here).
-
Open a Terminal window. This can be done by pressing command+Space (⌘+Space) and typing Terminal in the search bar and pressing Enter.
- You now need to install Homebrew. You can do this by typing the following command in the Terminal and pressing Enter:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- After the installation is finished, you will need to type the following commands in the Terminal, pressing Enter after each one:
echo export PATH='usr/local/bin:$PATH' >> ~/.bash_profile
- You can now install Python 3 using Homebrew. To do this, type the following command in the Terminal and press Enter:
- Now you need to install several dependencies for Pygame. Type the following commands in the Terminal, pressing Enter after each one:
brew install sdl sdl_image sdl_mixer sdl_ttf portmidi
brew tap homebrew/headonly
- You are ready to install Pygame. Type the following command in the Terminal and press Enter. Note that you will probably have to enter your password in the Terminal in order for the command to run:
sudo pip3 install hg+http://bitbucket.org/pygame/pygame
- To verify the installation, you will have to launch the correct version of IDLE. You can find this by opening Finder, clicking on Go in the top left of the screen, then clicking on the Go to Folder… option.
- In the search bar, enter /usr/local/Cellar/python and click Go.
- Navigate to the folder where Python 3 is located. It will probably be named in this format: 3.x.x.
- Launch the IDLE 3 app within the folder. When launched, it should be using Python 3.x.x.
- In the IDLE interpreter, type the following command and press Enter:
If the command completed without throwing any errors, you have successfully installed Pygame and found the correct version of IDLE to use for Pygame programming.
- To make finding this version of IDLE easier, you can create a Desktop shortcut for IDLE 3. If you want to do this, press and hold the control key and click on IDLE 3. It will open a Menu where you can select Make Alias and create another IDLE 3 icon. You can name this icon whatever you like, but make sure to drag it to your Desktop when you are done.
PyGame is a cross-platform set of modules in the Python programming language which means you can install it on any operating system and are designed to write video games. These modules also include graphics and sound libraries that are used together with the Python programming language. PyGame depends on python, which means we have to install python first before installing PyGame.
In this blog, I’ll be showing you how to install Python and then PyGame.
Installing Python on Windows
Downloading
To install Python, visit their website and download the latest version of python 3. If you are using a 32-bit system, choose the “Windows x32 installer,” If you are using the 64-bit system, choose the Windows x64 installer. You should note that the latest version of python doesn’t work on Windows 7 or earlier. This means that you can only use Python 3 on Windows 8 or later versions.
Python Releases
You should be able to download the executable installer. Once downloaded, run the Python installer.
Running the setup
When the installer opens, check the install launcher for all users(recommended) box and add python 3.7 to PATH(optional).
python install
Click on the customize installation and make sure the following boxes are checked.
- Documentation.
- Pip
- Tcl/tk and IDLE(to install tkinter and IDLE)
- Python test suite
- Py launcher
- For all users (requires elevation).
optional features
If you are satisfied with your check box choices, Click “next”.
You will be taken to the advanced options dialog, here you also have to check the following boxes:
- Install for all users.
- Add Python to environment variables (this can be done manually if you don’t check the box, but it will take more time. I recommend checking the box to save time).
Optional
- Associate files with Python (requires py launcher).
- Create shortcuts for installer applications.
You will realize that when you check the install for all users box, precompile standard library will automatically be checked. This is okay.
advanced options
Click “Install”.
The installation shouldn’t take very long, and you should see the setup was successful. You have completed your installation, and you can use Python.
Setup successful
Click “Close”.
Now that we have the latest version of Python running on our computer let’s get to the business that brought us here, that is, installing PyGame on Windows 10.
Installing PyGame on Windows 10
Locating Python folder
Launch file explorer and locate the python folder in the primary disk of your computer. To do this, use the shortcut commands Win+E or open the start menu and search explorer. File explorer should be the first option at the top left of the screen. Click it or press “Enter” to open it.
Python folder location
Copy the address of the Python folder. In my case it is:” C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.9″
Note: If you did not change the installation on the advanced settings, your python would be installed on the default settings of python installation. Please make sure to change the installation path on the advanced settings as shown above.
Copying the path/address of the python folder
You copy this by right-clicking on the Python name at the top of the screen on file explorer and click “copy address”.
Python folder address
If you changed your installation path, then your location should be as short as this:” C:\Program Files\Python39 ”
Copy this address.
Creating the path
Now that you have your address copied, we want to create a path in the environment variables. So we open the Control Panel. Do this by opening the start menu and search for the Control Panel. The Control Panel should appear at the top left of the screen.
Now click on it or press “Enter” on your keyboard to open it, then click on the System and Security > then Advanced System Settings.
control panel
It would help if you had the above panel opened. Click on Environment Variables.
Environment variables
Click “New” and paste the path copied from file explorer into the empty textbox generated on the Environment variables panel like the one above. You can also add Scripts Variable and Click “OK” twice to save the changes.
Python Variables
The two methods of installing Pygame
If all is now set, we can head into the last steps of installing PyGame. In this method, we can use the pip install or download the PyGame set up and install from the setup. The pip install gets PyGame from the internet and installs it on your machine during the setup.
Let’s start by using the setup method:
Head to this website Pygame Installer and download the appropriate installer version for your computer. Save the setup to downloads.
To ensure that we successfully installed Python, Open the command prompt as an administrator and run this command.
“python”
Python Version
This gives you your current installed python version, as shown above.
The next step is to navigate to the location of the downloaded PyGame. In our case:
C:\Users\name\> cd Downloads
Then type the command to install PyGame from the downloads folder as shown below:
C:\Users\name\Downloads> pip install
pygame-1.9.1.win32-py2.7.msi
Pygame Install Error
In one way or another, you might encounter this error if your “pip” version is of low version than the current version; hence consider running the below commands to upgrade.
python.exe -m pip install –-upgrade pip
Pip Upgrade
Finally, run the command below to install PyGame from the downloads folder.
C:\Users\Mozerian\Downloads\Programs>pip install pygame-1.9.1.win32-py2.7.msi
Let’s now look at the second. The first method can really give you some headaches since you will really do some debugging. I, therefore, recommend using this second option. To install PyGame from the command prompt:
First, open Command Prompt as an Administrator and navigate to the python installation location:
C:\Program Files\Python39
Then cd to Scripts
C:\Program Files\Python39>cd Scripts
You can now install Pygame here: type in “pip install pygame”, and Pygame will be successfully installed.
Install Pygame
PyGame successfully installed. How about running a few commands to launch PyGame and see if it works?
Getting Started with the PyGame
To run Pygame, we must first import all the functions that “Pygame” and “Sys” modules have. Go back to the python location and start python. Write the below commands:
>>> import pygame, sys
Getting started with Pygame
The next step will be to import all PyGame functions. Instead of calling modulename.functionname(), we can do this to import all functions.
from pygame.locals import*
We can then initialize Pygame by pygame.init(), and lastly, to display Pygame and run it, we need to set the window size. Type this command, and PyGame will launch.
pygame.display.set_mode
((400, 300))
Start Pygame
This will now launch the PyGame, on a window set to (400, 300).
PyGame
Conclusion
We have now successfully installed PyGame using the two methods. The only challenge you may overcome when doing this is the necessity to install python first. If you have no background in python, you do not have to worry since this tutorial guides you on a step-by-step basis. You can know to play around with PyGame, such as setting the Window title and any other thing you wish to. I hope this blog was helpful to you when it comes to installing PyGame on Windows 10.