36

I recently bought a new macbook and I've been trying endlessly to get pygame to work, but haven't succeeded yet. I'm getting pretty desperate and I could really use some help.

I've installed pygame 1.9.4 and even though I don't get any error messages when running pygame code, it won't show me anything but a blank screen. I'm using the following code to test it:

import pygame
pygame.init()

screen = pygame.display.set_mode((800,600))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

    screen.fill((255,0,0))
    pygame.display.update()

pygame.quit()

On my old macbook the test code gives me a red screen as expected. Both macbooks are running python 2.7.10.

Does anyone have any idea what I'm doing wrong? I think I installed pygame exactly like I did on my old macbook and the only difference seems to be the operating system.

edit 1: I write the script in Sublime Text and run the program in Terminal. Screenshot

edit 2: I got pygame working again by downgrading my operating system to macOS High Sierra.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Boudewijn Maas
  • 399
  • 1
  • 3
  • 10
  • 1
    can you execute it from a console window? also adding a screenshot of what you really get and how you call this programm would be helpful. Also Check this [guide how to ask good questions](https://stackoverflow.com/help/how-to-ask). – Jan Oct 09 '18 at 10:42
  • It could be the newer MacBook display does not work at 800x600; try another resolution. (@Jan: I am curious as to how a screenshot of a blank screen might help. But I guess it's worth a shot.) – Jongware Oct 09 '18 at 11:36
  • @usr2564301 I wasn't sure if anything loads at all. but if you are, then we got that sorted out ;) – Jan Oct 09 '18 at 11:39
  • well, It shows a red screen. Are you expecting something else ? ( tested on macbook air - 10.11.6) – Feelsbadman Oct 09 '18 at 11:41
  • 2
    I write the script in Sublime Text and run the program in Terminal. Screenshot: https://i.stack.imgur.com/0QVv6.png. I've changed the window size to 1000x1000, but unfortunately this didn't make any difference. – Boudewijn Maas Oct 10 '18 at 05:40

10 Answers10

26

Tested and works on macOS 10.15 Catalina, Python 3.7.5, PyGame 2.0.0 (pre-release as of this writing) and PyGame 1.9.6 (stable as of this writing).

Initially you need to decide if you want a stable release or a pre-release (unstable). If you decide to use the latest (and possibly pre-release/unstable) then just ignore the first step ("1. Find the latest stable release version") and use master branch at step 4 of the second step ("2. Install PyGame from source").

1. Find the latest stable release version

Go to PyGame's GitHub page here:

enter image description here

As you can see as of this writing the latest stable release is 1.9.6 so we hold this tag name for later.

2. Install PyGame on macOS from source

  1. Install some dependencies brew install sdl2 sdl2_gfx sdl2_image sdl2_mixer sdl2_net sdl2_ttf. This requires homebrew.
  2. Go to site-packages:
    • For virtual environment go to cd ~/.virtualenvs/myvirtualenv/lib/python3.X/site-packages where ~/.virtualenvs/myvirtualenv is the path to your virtual environment and python3.X is the version of your Python.
    • For system-wide installation go to /usr/local/lib/python3.X/site-packages where python3.X is the version of your Python.
  3. Delete any previous pygame, pip uninstall pygame (if a pygame directory exists in site-packages then remove it: rm -rf pygame*)
  4. Clone PyGame from GitHub:
    • git clone https://github.com/pygame/pygame.git for the latest (possibly not stable version).
    • git clone -b 1.9.6 https://github.com/pygame/pygame.git --single-branch for the latest (1.9.6 is the tag name of the latest stable release, as of this writing, see "Find the latest stable release version above")
  5. Go into the newly cloned pygame directory: cd pygame.
  6. Run python setup.py --config --auto --sdl2.
    • If you get problems with this command, some users below mentioned that single hyphens worked for them, therefore try: python setup.py -config -auto -sdl2.
    • If you get any problems regarding Python2/3 and you are targeting Python3 try to change python setup.py --config --auto --sdl2 to python3 setup.py --config --auto --sdl2.
  7. Run python setup.py install (it will take a while).
    • If you get any problems regarding Python2/3 and you are targeting Python3 try to change python setup.py --config --auto --sdl2 to python3 setup.py --config --auto --sdl2.

Now PyGame should work as expected on macOS.

Rafael
  • 7,002
  • 5
  • 43
  • 52
  • 1
    Thank you so much! This was driving me mad... but worked exactly as you described. (Catalina, Python 3.7.5 (pyenv), pygame 2.0.0) – rhedak Dec 22 '19 at 03:30
  • 1
    When running `python setup.py -config -auto -sdl2` I get the error `sh: pkg-config: command not found`, then `WARNING: "pkg-config freetype2" failed!`. I installed `pkg-config` with `homebrew` but then encountered `WARNING, No "Setup" File Exists` – ChumiestBucket Dec 24 '19 at 16:25
  • 5
    on my osx (10.15.2), I should use: python setup.py -config -auto -sdl2 。For detailed info, pls. refer to: https://www.pygame.org/wiki/MacCompile – Liang Lyon Jan 02 '20 at 04:39
  • 1
    I had to use single hyphens, like @LiangLyon, but many thanks to Rafael for the fix. – shane Feb 16 '20 at 17:01
  • upon running `python3 setup.py -config -auto -sdl2` I get the error `WARNING, No "Setup" File Exists, Running "buildconfig/config.py" Using Darwin configuration... /bin/sh: pkg-config: command not found /bin/sh: pkg-config: command not found /bin/sh: pkg-config: command not found` – ChumiestBucket Mar 18 '20 at 17:54
  • I had to use 'python3 setup.py -config .auto -sdl2' and 'python3 setup.py install' bc it would throw an error every time when using 'python' – andre Apr 12 '20 at 11:28
  • Thanks @andre for your feedback. I will update the answer with your input so other people will find it useful! – Rafael Apr 12 '20 at 11:39
  • I believe a simpler fix is now available simply by using pip to install pygame 20.0.0.dev4 (which will be superseded soon, I presume) – GregD Apr 19 '20 at 18:15
19

I also face this problem on the macOS Catalina. Actually it is a version problem of pygame. Firstly I have installed with the following command:

pip3 install pygame

An only blank screen was showing. But later on, I have changed the version with the following command:

pip3 install pygame==2.0.0.dev4

With this version my problem was solved. It was working perfectly.

Zakaria
  • 445
  • 4
  • 10
17

LATEST UPDATE for MacOS 10.14.1

If you download the official macOS x64 installer package of Python 3.7.2 from the official python.org page and type pip3 install pygame, it works.

There's an issue with MacOS. It should be possible to fix in SDL.

The pygame issue is here: https://github.com/pygame/pygame/issues/555

The homebrew issue is here: https://github.com/Homebrew/homebrew-core/issues/33016

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
René Dudfield
  • 591
  • 4
  • 10
  • Thanks for the guidance. This just helped me. – Michael Welch Mar 15 '19 at 01:39
  • For clarity's sake: **downloading the latest Python version from the official website will work**. Homebrew's Python 3 does not seem to be solving this issue, yet. – glhrmv Mar 27 '19 at 23:00
  • 2
    Gave a try to Python 3.7.3 form their [official website](https://www.python.org/downloads/) with the third iteration of Mojave (MacOS 10.14.3) and pygame 1.9.4, no joy here :/ I have tried 3.7.2 before on macOS 10.14.1 without any better result. Has anyone else been able to get it work? – Attila Apr 04 '19 at 22:53
5

I ran into this issue using macOS Catalina version 10.15.2, Python 3.7.2 and pygame 1.9.6. I upgraded to Python 3.8.1, Pygame 2.0.0.dev6 and it is now working as expected.

mario
  • 51
  • 1
  • 2
  • Thank you--I'm also on Catalina 10.15.2, and didn't want to downgrade my Python. Since I'm just learning PyGame, I'm not too particular about the version (assuming it's at least moderately stable). – Daniel 'Dang' Griffith Apr 27 '20 at 01:01
  • 1
    I've been using Pygame 2.0.0.dev6 for a couple of months now and - even tough is not an official release yet - It is behaving very stable for me. – mario Apr 28 '20 at 12:36
  • In my case, yes, upgrade to Pygame 2 removes the blank screen issue... except there appear to be key event handling differences between Pygame 1 and Pygame2. My code no longer works! – Jacob Lee Mar 25 '21 at 22:18
3

Edit with working workaround: see bottom of post


I get the same problem on a MacBook Pro (Retina display) with python3.7 and MacOs Mojave. For the sake of simplicity, I did all my tests with your exact same code and pygame 1.9.4.

On (two) Windows computers, the program works and shows a red window on :

  • python2.7
  • python3.6
  • python3.7

Hence it doesn't seem to be a Python version problem, I lean toward a MacOS Mojave problem.

Mojave introduced 'Dark mode' which automatically tint windows. When dark mode is disabled, I get a blank screen. When it is enabled, I get a dark screen. Might it be that MacOS takes control of the screen display and fiddles with the way pygame controls the screen? It is just a theory but I don't know how to test it.


Edit: I tried a pygame program that records and prints the position of the screen on which you clicked. It does output the position in the console, alas it doesn't display anything. Apart from the faulty display, pygame seems to work properly.


Edit: For now, using the Python version from miniconda and using CogSci's pygame seems to work.

adam
  • 31
  • 4
3

I tried upgrading my old macbook to OS Mojave to see if pygame would stop working, and it did!

I guess Mojave isn't compatible with pygame (yet).

Thanks for the help!

Boudewijn Maas
  • 399
  • 1
  • 3
  • 10
3

I've tried everything but nothing worked except this:

pip install git+https://github.com/pygame/pygame.git

This installs the tips of the pygame. It worked for me. Give it a try.

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
  • 1
    it worked for me! Thanks! I have got the following: pygame 2.0.0.dev7 (SDL 2.0.12, python 3.7.7) – Weiming Apr 18 '20 at 00:37
2

I ran into this problem with the following environment: Python 3.7, Mojave 10.14.2, Pygame 1.9.4

I found that downgrading to Python 3.6 fixes the issue

$ brew unlink python
$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

That should fix it, if you get a circular dependency problem you should then run:

$ brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

Details at https://apple.stackexchange.com/questions/329187/homebrew-rollback-from-python-3-7-to-python-3-6-5-x

  • 2
    How would you fix this for 2.7 if you want to carry on using this? this is only for 3.6 .... :( – Marin Feb 12 '19 at 02:19
1

I had the same issue, got it to work by downgrading python to 3.6.5.

> brew unlink python  brew install --ignore-dependencies
> https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
> brew switch python 3.6.5_1 pip3 install pygame

If you already have python 3.6.5 install I guess you can just use switch.

0

Some macs need updates. If you go to system preferences and you go to software updates, you could try updating to see if it needs any updates. When you update, you can check to see if your code works now because it looks like your code is correct.

Joe
  • 1