简体   繁体   中英

PyGame not working in Eclipse using Python3.4

I am using Linux Mint 17 (Ubuntu 14.04) and already got pygame 1.9.2 working in Eclipse on my Windows PC. Now I also installed it on this machine, but it doesn't work. Thus there is no apt-get for python3-pygame I downloaded the source from https://bitbucket.org/pygame/pygame and built and installed it without getting errors.

When running the following on the command line I receive no error:

import pygame
pygame.init()

If I try the same in an Eclipse PyDev project I get the error: "Undefined variable from import: init".

First I thought that my Interpreter was not set up correctly, but the path where pygame is installed is added to the libraries (/usr/local/lib/python3.4/dist-packages). I realized that for Python2.7 pygame is installed in /usr/lib/pytho2.7/dist-packages instead, but this directory doesn't exist for python3.4 on my machine.

Since it seems to work on the command-line it must have something to do with the Eclipse or Pydev settings, right?

UPDATE: Ok, now things are getting really confusing. I found out that pygame indeed is also working from within eclipse if I run the project, but only giving me these error messages for eg. pygame.init(), pygame.QUIT, pygame.K_ESCAPE, pygame.KEYDOWN. I find it very strange, because pygame.time.Clock() or pygame.display.set_caption() don't give error messages. So I only get undefined variable from import errors (also see here How do I fix PyDev "Undefined variable from import" errors? ).

Use sudo pip3 install pygame , pip3 should work assuming you have python3 and setuptools installed.
You can install pip3 using sudo apt-get install python3-pip and then use sudo pip3 install package_name to get whatever you want.

The only thing which worked for me (I spent two days now finding out what the problem is) is to add "pygame" to the forced builtins for the Interpreter (Window -> Preferences -> PyDev -> Interpreter -> Python Interpreter -> Forced Builtins Tab -> New -> pygame .

This is only a workaround, but at least I am rid of those errors for now and auto-completion still works. I didn't find any other solution that worked for me so far.

For me the problem was resolved by importing * . instead of:

import pygame

try this:

from  pygame import *

now instead of calling init() by saying pygame.init() just use init() also you should not have to explicitly call using pygame. for the most part.

NOTE: after playing around with this in Eclipse I did end up having to use both:

import pygame
from pygame import *

for whatever reason one of my functions only works if I have pygame.even.get() but the test of my code does not need to use pygame. (I am sure there is a perfectly good reason but I am still very new to python)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM