【问题标题】:Pygame not importing correctlyPygame 没有正确导入
【发布时间】:2014-12-06 21:11:37
【问题描述】:

我是 Python 新手,我正在尝试开始使用 Pygame。

当我运行这段代码时:

import pygame
pygame.init()

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\Jason\PycharmProjects\Game\myapp.py", line 6, in <module>
    pygame.init()
AttributeError: 'module' object has no attribute 'init'

但是当我尝试查看是否不小心导入了另一个文件时:

import pygame
import inspect

# Initialize the game engine
print(inspect.getfile(pygame))
pygame.init()

我收到这个奇怪的错误:

Traceback (most recent call last):
  File "C:\Users\Jason\PycharmProjects\Game\myapp.py", line 5, in <module>
    print(inspect.getfile(pygame))
  File "C:\Python34\lib\inspect.py", line 518, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'pygame' (namespace)> is a built-in module

如何导入正确的 Pygame?

我在 Windows 7 上使用 3.4,在 C:/Python34 中使用 Pygame 1.9.2a0(用于 3.2)。

谢谢

【问题讨论】:

  • 您是否自己在某个地方创建了一个名为“pygame”的文件或目录?你是如何安装 pygame 的?
  • 我使用了默认安装程序。我没有创建任何名为 pygame 的东西
  • 我认为问题在于版本。我刚刚安装了Python 3.2,但是如何配置pygame呢?
  • 这里有 Python 3.4 的 pygame 版本,你可以试试。无论您使用什么版本的 Python,您都需要为该 Python 版本安装一个 Pygame 版本。
  • 我确实为 3.2 安装了它,我刚刚安装了 Python 3.2

标签: python pygame python-import


【解决方案1】:

这一切都与版本有关。就这么简单。

原来的答案在这里:https://stackoverflow.com/a/23484831/2486953

我必须从这里下载 64 位 版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

感谢@BrenBarn 的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多