i keep getting the error 'module' object has no attribute 'init' [duplicate]

久未见 提交于 2019-12-17 17:06:13

问题


Especially when i run it from an external python file and just run it using IDLE or Pycharm..Please Help...but at times it works with in the interactive shell and then something happens and it starts its problems ....I simply typed

import pygame
x = pygame.init()
print(x)

C:\Python33\python.exe C:/Users/Home/Desktop/pygame.py Traceback (most recent call last): File "C:/Users/Home/Desktop/pygame.py", line 1, in import pygame File "C:\Users\Home\Desktop\pygame.py", line 2, in x = pygame.init() AttributeError: 'module' object has no attribute 'init'

Process finished with exit code 1.


回答1:


The problem is that you named your file pygame.py.

If you run it and you want to import pygame, it will import your file C:\Users\Home\Desktop\pygame.py and not the installed pygame module.

So just use another filename (and delete C:\Users\Home\Desktop\pygame.py and any other leftovers).




回答2:


pygame.init() initializes the pygame module, you don't assign it as a variable.

import pygame
pygame.init()

And that's it.

edit: As sloth mentioned: you also don't name your python projects the same name as modules, especially if you plan on importing them.



来源:https://stackoverflow.com/questions/28231200/i-keep-getting-the-error-module-object-has-no-attribute-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!