如何在 Windows、macOS 和 Linux 上安装 Pygame

如果您知道如何从终端窗口运行 pip(Python 的软件包安装程序),请运行:

pip install pygame

如果您不知道如何在终端中使用 pip,那么您可以通过运行以下命令在 Python 的交互式 shell(或 REPL)中安装:

import subprocess, sys; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pygame'])

(这是方法 pipfromrepl 从交互式 shell 安装 Python 包。)

对于 Python 版本 3.6 至 3.10,此过程适用于 Windows、macOS 和 Linux。 请注意,截至 2023 年 1 月,Pygame 维护者尚未更新 Pygame 以在 Python 3.11 或 3.12 上运行。 但是,有一个 .whl“wheel”文件可用于在 Windows 上的 Python 3.11 上安装 Pygame。 从命令提示符或 Windows 终端窗口运行:

pip install https://inventwithpython.com/pygame/pygame-2.1.2-cp311-cp311-win_amd64.whl

或者从交互式 shell 运行它:

import subprocess, sys; subprocess.run([sys.executable, '-m', 'pip', 'install', 'https://inventwithpython.com/pygame/pygame-2.1.2-cp311-cp311-win_amd64.whl'])

您可以通过从交互式 shell 导入来测试 Pygame 是否安装成功。 如果没有出现错误提示,则安装成功:

>>> import pygame
>>>

随着新版本的 Python 和 Pygame 的推出,这篇博文将会更新。

阅读更多

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注