pygame.init() shows as undefined variable after in

2019-07-22 21:07发布

问题:

I'm trying to write a sample Python application using Pygame:

import pygame, sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello world!')
while True: # main game loop    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

However, PyDev complains that init() is an undefined variable even though it recognizes the Pygame library. I'm using Python 2.7.6 as the interpretor.

Here's the stack trace:

Traceback (most recent call last):
  File "/Users/dannychia/Documents/workspace/PygameSample/PygameSample.py", line 6, in <module>
    import pygame, sys
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 95, in <module>
    from pygame.base import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found.  Did find:
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so: no matching architecture in universal wrapper

Anyone have a solution?

回答1:

Here are some suggestions. One of these should solve the problem:

-I see that your release is the 32 bit version. Considering you probably have a 64 bit computer, it is possible that you have a 64 bit python distribution. This error is sometimes caused by bit rate differences. You can check the bit rate of the python distribution like so:

import platform
platform.architecture()

-According to this question, uninstalling pygame, and redownloading and installing it seems to fix the problem sometimes.