繁体   English   中英

为python2.7安装pygame模块

[英]install pygame module for python2.7

我已经为 Python3 安装了 pygame(在 Windows 上)
但是现在我想运行一个用Python2.7写的源代码
当我运行代码时出现此错误:

导入错误:没有名为 pygame 的模块

我如何将 pygame 用于 python2.7?(我该如何修复它?)

谢谢

我刚刚做到了,它非常简单。 我相信它在任何操作系统中都是相似的,因为我们正在谈论 python。但我在 Linux 上做了,因为它是我大部分时间使用的操作系统。

  1. 已安装 Python 2(在 windows 上,您可以在此处获取安装程序: https://www.python.org/download/releases/2.7/ ),在 Linux(类似 Debian)上:
sudo apt-get update
sudo apt-get install python2.7
  1. 安装了 pip(可能在 Windows 上,它将与 python 2 一起安装在安装程序中,如果没有,您也可以按照下面的说明进行操作)。

但是在 Linux 上,有你的终端,然后:

  • 运行此命令以下载 Python 2.7 的 pip 安装程序(在 Windows 上,wget 可能仅适用于 powershell,或者将 url 粘贴到浏览器中,您可以下载它)
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
  • 然后安装它:
sudo python2 get-pip.py
  1. 最后你可以有效地安装它:
pip2 install pygame

最后检查它在 python 中的导入,在终端中:

$ python2.7

您将能够看到 python 2 打开:

Python 2.7.18 (default, Mar  8 2021, 13:02:45) 

然后尝试导入 pygame:

import pygame

你应该看到:

pygame 2.0.3 (SDL 2.0.16, Python 2.7.18)
Hello from the pygame community. https://www.pygame.org/contribute.html

我通过对 Python 的不同版本使用 pip 解决了我的问题

> py -2 -m pip install pygame
> py -3 -m pip install pygame

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM