Python 编译错误。AttributeError: 'module' 对象没有属性 'init'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4065800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:05:08  来源:igfitidea点击:

compilation error. AttributeError: 'module' object has no attribute 'init'

pythoncompiler-errors

提问by SunnyShah

Here is my small program,

这是我的小程序,

import pygame
pygame.init()

Here is my compilation command.

这是我的编译命令。

python myprogram.py

蟒蛇我的程序.py

Compilation error,

编译错误,

  File "game.py", line 1, in 
    import pygame
  File "/home/ubuntu/Documents/pygame.py", line 2, in 
    pygame.init()
AttributeError: 'module' object has no attribute 'init'

I have pygame installed in my ubuntu, It is installed in

我的 ubuntu 中安装了 pygame,它安装在

/usr/lib/python2.6/dist-packages/pygame

I found tht from IDLE, If I execute both of this statements, It works fine.

我从 IDLE 中发现,如果我执行这两个语句,它工作正常。

采纳答案by Ned Batchelder

Delete the "pygame.py" file in your Documents folder, it is shadowing the real pygame you've installed.

删除 Documents 文件夹中的“pygame.py”文件,它隐藏了您安装的真实 pygame。

It looks like you first saved your small test program as "pygame.py", then renamed it to "game.py".

看起来您首先将您的小测试程序保存为“pygame.py”,然后将其重命名为“game.py”。

回答by lol

Rename pygame.py to another_name_as_pygame.py and this is your new Code:

将 pygame.py 重命名为 another_name_as_pygame.py,这是您的新代码:

import pygame
from pygame.locals import *
pygame.init()

回答by ARCAON

  1. Here is my compilation command:python myprogram.pybut you have error in File "game.py", line 1, inoO

  2. I was have same problem and solve it by renaming of main .py file. He can't be the same as module i.e pygame

  1. 这是我的编译命令python myprogram.py但是您在File "game.py", line 1, inoO 中有错误

  2. 我遇到了同样的问题,并通过重命名主 .py 文件来解决它。他不能和模块一样,即pygame