简体   繁体   中英

ImportError: no module named pygame?

I can import pygame through the command line, and through all my other prorams, but it brings up an error in my crosshairs program:

Traceback (most recent call last): File "C:\\Users\\Family\\Desktop\\pys\\Crosshairs(2).py", line 1, in import pygame ImportError: No module named 'pygame'

I have no idea what the heck it's talking about, but I'm thinking this could be windows vista being a crap hole(vista is verry glitchy) but I'm not too sure. Does anyone know what the problem is? If you need it, here is the code:

import pygame
import math
import sys

WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)
BGCOLOR = WHITE
WINDOWWIDTH = 640
WINDOWHEIGHT = 480

class Control(object):
    def __init__(self):
        self.bullet_holes = []
        self.screen = pg.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        self.done = False
        self.clock = pg.time.Clock()

    def update(self):
        vis = False
        pygame.mouse.get_visible(vis)
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        pygame.draw.circle(self.screen, RED, (320,240),50,10)
        pygame.draw.circle(self.screen, WHITE, (320,240),40,10)
        pygame.draw.circle(self.screen, RED, (320,240),30,10)
        pygame.draw.circle(self.screen, WHITE, (320,240),20,10)
        pygame.draw.circle(self.screen, RED, (320,240),10,10)
        pygame.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), 
                                             (self.mousex + 2000, self.mousey))
        pygame.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000),
                                             (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pygame.draw.circle(self.screen,BLACK,bullet_pos,5)

    def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and
                                          event.key==pg.K_ESCAPE):
                self.done = True
            elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                self.bullet_holes.append(event.pos)
                pygame.image.save(self.screen,'Highscores.png')

    def main_loop(self):
        while not self.done:
            self.update()
            self.event_loop()
            pygame.display.flip()
            self.clock.tick(60)


if __name__ == '__main__':
    game = Control()
    game.main_loop()
    pygame.quit()
    sys.exit()

Your pygame module is not in the path and cannot be found. Correct this by moving it or using PySys_SetPath().

You could do it with sys.path.insert command:

import sys
path_to_folder = os.path.abspath("c:\\Your\\destination\\folder")
sys.path.insert(0, path_to_folder)

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