How to install Pygame using PIP or an IDE

How to install Pygame

We will see in this tutorial how to install the pygame module on Windows or MAC using pip or the PyCharm editor.

Introduction

When you’re new to Python programming, the best way to improve your skills is to create mini-games. This allows you to have fun while learning the basics of programming. To develop a game in Python, there are several modules that make it easier to work with graphics (such as rectangles, circles, images, etc…) and sounds. Pygame is one of them.

Pygame is a Python wrapper for the SDL (Simple DirectMedia Layer) library. SDL provides cross-platform access to the underlying multimedia hardware components of your system, such as sound, video, mouse, keyboard and joystick. You can therefore program games and Python programs with Pygame that are supported on different platforms.

By default, Pygame is not installed on the Python programming environment. We will therefore see in this guide the best instructions for installing it on your system.

If you wish to deepen your knowledge in the Pygame Module, I invite you to read this book :

As an Amazon Associate I earn from qualifying purchases. If you purchase a product by using a link on this page, I’ll earn a small commission at no extra cost to you

Sale : -25% off!
Beginning Game Development with Python and Pygame: From Novice to Professional (Expert's Voice)
43 Reviews
Beginning Game Development with Python and Pygame: From Novice to Professional (Expert's Voice)
  • McGugan, Will (Author)
  • English (Publication Language)
  • 340 Pages - 10/19/2007 (Publication Date) - Apress (Publisher)

How to install Pygame for Windows

Install Python

To run the Pygame module, we must have a version of Python on our machine. If you haven’t already done so, it is available here :

https://www.python.org/downloads/

How to install Pygame - python installation
Download Latest Version of Python

Click on the version of Python you are interested in and press download. You can install the latest stable version of Python.

Once the download is complete, press the run button. Check the box ”Add Python 3.5 to PATH”.

Make sure the checkboxes for “Optional features” are checked. For the rest, you can leave them as they are. To finalize the installation, press the install button for your computer to complete the installation.

Install Module Pygame

You can get a version of Pygame via this link :

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Take the latest version available and click on the link to download the .whl file to your computer.

pygame‑1.9.6‑cp39‑cp39‑win32.whl

You can choose between a 32bits or 64bits version (in this example, we will start with the win32 version).

To install the previously downloaded Pygame module, we need to access the Windows command line.

To access it, right-click on the started menu and click on Execute. Then type on the text box “cmd“.

Then go to the directory where you installed the Pygame module (by default in the Downloads folder).

Once you are in the right folder, type the following command prompt:

# use the cd command to move between folders

C:\Users\amiradata\> cd Downloads
C:\Users\amiradata\Downloads> pip install pygame‑1.9.6‑cp39‑cp39‑win32.whl

To check that the installation has worked, type the following command prompt:

# Execute python on command line and import Pygame

C:\Users\name\Downloads> python
>>> import pygame

If you do not see any error messages, the installation went well.

How to install Pygame for OS X

The first step to do is to access the command line. To do so, click on the Spotlight icon and type “terminal” to find it.

We will install XCode (which is an Apple tool for creating Mac and iOS applications. Once the installation is complete, type the following command prompt:

$ xcode-select --install

Install Homebrew

Homebrew is a package manager for Mac that simplifies the installation of many different software or packages such as Git, Python or Pygame. Homebrew allows you to avoid possible security issues related to the use of the sudo command to install software like Node.

To install it, copy and paste this on the command prompt:

# Install Brew 

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You will also need to install Homebrew Cask :

# instal HomeBrew Cask

$ brew install caskroom/cask/brew-cask

Install pygame and these Requirements

To install pygame, we must first install all the pygame dependencies. To do this, please run the following commands one at a time:

# Install all requirements

$ brew cask install xquartz
$ brew install python3
$ brew install python
$ brew linkapps python3
$ brew linkapps python
$ brew install git
$ brew install sdl sdl_image sdl_ttf portmidi libogg libvorbis
$ brew install sdl_mixer --with-libvorbis
$ brew tap homebrew/headonly
$ brew install smpeg
$ brew install mercurial
$ pip3 install hg+http://bitbucket.org/pygame/pygame

As you can see, we have installed python 3 and the pygame module. To check that the installation has been successful, type the following commands:

$ python3
>>> import pygame

If you don’t get any error messages, it means that the installation was successful.

How to install Pygame using PyCharm IDE

PyCharm is an integrated development environment (IDE) used in computer programming, specifically for the Python language. PyCharm is cross-platform, with Windows, macOS and Linux versions.

To start opening Pycharm and create a new python project. Then create a python file by right-clicking on the project then New then Python file. Name your python file as you wish.

In your python file, type the following line :

# import module pygame

import pygame

You will see that the pygame is underlined in red. To install pygame, move the mouse over the red underlined area and click on “install package pygame“.

Wait for Pycharm to install pygame and that’s it !

How to install Pygame using Pycharm
PyCharm IDE : How to install pygame module

Conclusion

In this tutorial, we have seen how to install pygame on Windows, OS X and using an IDE like PyCharm. Now it’s up to you to create your first 2D mini-game!

Here is an example of what you can do with Pygame :

Don’t hesitate to tell me in the comments if you have problems with the pygame installation. And also, I would be happy to see your future projects on pygame!

See you soon 🙂

Back to the python section

Published
Categorized as Python

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published. Required fields are marked *