import pygame fails

2019-09-09 11:02发布

问题:

I have installed python 2.6.6 and pygame 2.6 from here:
http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi

However, when I try to run this simple program, I get a an error:

import pygame
pygame.init()
pygame.quit ()

The error says:

unresolved import:pygame

What could it be?

Edit: When I am doing

import pygame

in C:\Python26\python.exe , the interpreter, it works ok.
So it is probably an Eclipse problem.
Anyone has an idea what to do?

回答1:

Before

 import pygame

add the lines

import sys
print sys.path

if the path to pygame isn't shown, then you've don't have the PYTHONPATH environment variable set correctly.



回答2:

sudo pip install pygame

And then try:

import pygame

That should work for you.



回答3:

If Eclipse can not find Pygame when you import it. There is an issue with Pygame being in Python's path. If you are trying to run Pygame through Eclipse like I am. The easiest way is to uninstal Pygame. Download Pygame from here.

http://www.lfd.uci.edu/~gohlke/pythonlibs/

Make sure it is the correct version of Pygame for your version of Python. When installing if it does not find the path by itself you will need to place it in yourself. My version of Pygame placed itself in the correct path.

To check Python's path with out going in to preference. You can type the code.

import sys
print (sys.path)

If it is showing it in your path and it is still not working. You have the wrong version for you current Python. If every thing is right then your code should work. You can use the code below to print your Pygame version.

import pygame
pygame.init()
print (pygame.ver)