【问题标题】:Python program throws error because it says it cannot find pygame even though I have imported it in the programPython程序抛出错误,因为它说它找不到pygame,即使我已经在程序中导入了它
【发布时间】:2020-05-18 03:46:44
【问题描述】:
import sys

import pygame

def run_game():
    pygame.init()
    screen=pygame.display.set_mode((1200,800))
    pygame.display.set_caption("Space Battle")

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

        pygame.display.flip()
run_game()

我收到此错误:

Traceback(最近一次调用最后一次):

文件“C:/Users/Dell/PycharmProjects/test/test.py”,第 3 行,在
导入 pygame
ModuleNotFoundError:没有名为“pygame”的模块

我已经安装了 pygame 并在 windows 上的 cmd 上执行了导入命令。

我该如何解决这个问题?

【问题讨论】:

    标签: python python-3.x import pygame


    【解决方案1】:

    如果您的电脑中有多个 python 编辑器,请确保将 pygame 安装到正确的 python 编辑器中

    【讨论】:

    • 你是对的。该程序在 IDLE 中运行,但我使用的是 PyCharm,那么如何在 PyCharm 中安装 pygame。
    • 转到 Pycharm 所在的位置,应该有一个“pip.exe”文件,可能在一个名为 Scripts 的文件夹中。复制'pip.exe'的地址,cd到cmd中的地址,然后在cmd中运行'pip install pygame'。
    【解决方案2】:

    出现这种问题的原因有两个:

    1. 如果您的系统中有更多的 Python 版本,并且您尚未设置 Python 环境以使用默认的 Python 版本。
    2. 安装包或库后,有时编辑器响应不佳。在这种情况下,只需关闭编辑器并重新启动它。大多数情况下,它有助于获取库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 2019-05-09
      • 2013-06-24
      • 2021-03-25
      • 1970-01-01
      • 2020-03-15
      • 2023-01-25
      • 2012-05-16
      相关资源
      最近更新 更多