Solve the problem that the Mac cannot successfully install pygame, cannot change the window background color, and does not display the spaceship image

Solve the problem that the Mac cannot successfully install pygame, cannot change the window background color, and does not display the spaceship image

In the process of learning the book "Python Programming From Entry to Practice" , follow the steps in the book, when we use pip3 install --user hg+http://bitbucket.org/pygame/pygame in the terminal, the command cannot After installing pygame, or after successfully installing through the pip3 install pygame command , call the import pygame module to run the program in the "Alien Invasion" code, and you will find that the python application icon keeps beating or not moving in the program dock (if the system preferences When the program is not opened, it will not move if it jumps), although you can uninstall python at this time (a large part of the reason is because your python is downloaded and installed from the official website), and then python and pygame must be completely installed with the terminal through the homebrew method Solution (there are in the order book, but you may encounter the following problems).

Although you can successfully install pygame with the pip3 install pygame command in the terminal, and you can successfully run the pygame window. However, in the subsequent writing process, new problems will arise. The background color of the pygame window cannot be changed , and the spaceship image cannot be displayed . By testing a series of methods, such as re-installing python on the official website (what 32-bit and 64-bit must match), installing python directly through commands (the homebrew method in the book), and changing the pygame.event.get() in the code , Or installing a low version of python, etc. can not solve these problems.

There are two reasons for this problem. One is the compatibility problem of the Mac system (the method to reduce the Mac system is not to try), and the other is if you follow the installation tutorial in this book to install homebrew and then use the brew install pytion method and Not suitable for the current version of python.

Therefore, the following will summarize a practical and effective method to solve all the above problems. Avoid too many detours for beginners like me.

The overall idea is that we need to install python and pygame through another method. Here we are using anaconda (just as a very complete python software, after installation, you can save you the steps of installing other libraries, in fact, I also refused at the beginning, because it is in English) .

The prerequisite for installing anaconda is that we need to completely uninstall the installed pip3, pygame, python3. (Forgive me for not being able to write out the specific uninstallation method here, because I finally can't delete the old version of pip3 and chose to reset the Mac. You can search for the uninstallation steps by yourself.)

After the uninstallation is complete, we can install anaconda happily (although it is an English software, don't worry, it's easy to use, search for the installation method by yourself, and search for the slow download solution if the download is slow). Anaconda comes with python. After anaconda is installed, python and some libraries will be installed naturally, and there is no need to install anything else. Next, we install pygame directly , open the terminal, enter conda install pip, and it may prompt to update, type y and press Enter. Then after updating pip, enter pip install pygame (note that all installation commands do not need to distinguish between python and python3 or pip and pip3). After the installation is successful, you can enter it in the terminal

python -m pygame.examples.aliens command to test if the game screen appears, congratulations on your successful installation. Finally, if you use sublime as a text editor, you need to reconfigure the path , the python path is no longer in the book , you need to re-check through the type -a python3 command (copy the first path). Well, continue to learn python.

If you have any questions, please leave a message.

Reference: https://cloud.tencent.com/developer/article/1606186 Solve the problem that pygame cannot be successfully installed on Mac, the background color of the window cannot be changed, and the spaceship image is not displayed-Cloud + Community-Tencent Cloud