【问题标题】:`import pygame` shows no errors even when pygame is not installed即使未安装 pygame,`import pygame` 也不会显示错误
【发布时间】:2015-06-01 15:19:01
【问题描述】:

我一直在安装、卸载和重新安装 pygame,因为我一直在尝试运行一个程序(使用 pygame)。但是,无论何时我安装了 pygame,该程序都无法运行。

Traceback (most recent call last):
  File "C:\Users\Ryan\Desktop\en\snake.py", line 28, in <module>
    class Segment(pygame.sprite.Sprite):
AttributeError: 'module' object has no attribute 'sprite'

(据说我收到此错误时未安装 Pygame)

真正让我困惑的是,当我在 IDLE 中正常输入 import pygame 时,即使我刚刚卸载它,我也没有收到任何错误。

  • Windows 8 64 位
  • Python 3.4.3
  • Pygame-1.9.2a0.win32-py3.4.msi(pygame 文件名)
  • Python 安装在 C 盘 (C:\Python34)
  • 无论何时安装 Pygame,它都在 D 盘上
  • 将 Python 目录添加到 Path

Here's我正在关注的视频教程的屏幕截图,它显示有两个安装路径。还有here's mine,它只显示一条路径。我不确定这是否很重要,但我只是想确定一下。

【问题讨论】:

  • 我通常导入模块的方式是将它们放在 C:/Python27/Lib/site-packages 中。我从来没有听说过在 D: 上安装(听起来像闪存驱动器)。
  • @bcdan 这是第二个硬盘,抱歉应该澄清一下。
  • 我认为模块需要安装在安装python的驱动器中。因此,如果 C:/Python34 是 Python 的安装位置,则该模块需要位于同一目录中的 Lib/site-packages 文件夹中。这是默认目录。
  • @bcdan 当我安装 Pygame 时,它​​只允许我选择一个安装路径,即D:/,我可以将我的文件复制/粘贴到Lib/site-packages 中吗?我实际上已经尝试过了,当我输入 import pygame 时,我得到 this
  • 安装 pygame 时,我只是简单地拖放到文件夹中。我认为这应该可以正常工作。我不认为 pygame 需要 分期付款。但是,使用 Python 2.7。

标签: python installation pygame


【解决方案1】:

打开你的python解释器和import pygame 然后您可以打印 pygame 以查看正在加载的确切内容,应该类似于:&lt;module 'pygame' from '/Library/Python/2.7/site-packages/pygame/__init__.py'&gt; (重复上述步骤,直到导入pygame失败), 关闭提示并安装 pygame。尝试再次执行它。如果问题仍然存在,您可以在您尝试运行的游戏中编辑一些文件并添加两行:import sysprint sys.path 并检查路径列表以确保没有其他捆绑的 pygame 安装正在使用的游戏,而不是您安装的 pygame。

【讨论】:

  • 我什至不能输入 import pygame 并且 not 收到错误消息。我认为问题在于将 Pygame 添加到我的PYTHONPATH,但我不知道该怎么做。
  • 详情请看我的回答。
【解决方案2】:

修改 Josep Valls 的回答:

您需要更改sys.path。这可以通过以下代码完成:

import sys
sys.path.append('''directory where pygame is''')
import pygame

sys.path 变量告诉 Python 在哪里寻找模块。通过追加添加路径,您是在告诉 Python 在那里查找导入的模块。生成该指针后,您可以导入 pygame.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多