How to fix error: " No module named 'pygame' "

I installed the pygame module and it works just fine when I try to run it from PyCharm or Sublime Text, but when I try to run it from console or IDLE it says: " Error: No module named ‘pygame’ ".I should mention that my python 3.8 is not installed on it’s default location, but rather on other partition in the custom folder. I also had problems with PATH and python when I first installed python.

Add Comment
5 Answer(s)

Did you do import pygame at the beginning of the script? That may be the problem.

Answered on July 16, 2020.
Add Comment

show us your code and we’ll try to help you out

also, if you are indeed imported pygame to you code, remember to add

import sys  print sys.path 

to the first three lines and before the import pygame 🙂

import sys print sys.path import pygame 

i took this from here.

Add Comment

Here’s my main code. Idk how to format it well…

``` import sys print(sys.path) import pygame import klase import funkcije import os import neat

WIN_W = 800 WIN_H = 400

gen = -1

def main(genomes, config):

global gen gen += 1 nets = [] ge = []  floor = klase.Floor(0) dinos = [] cactis = []  for _, g in genomes:     net = neat.nn.FeedForwardNetwork.create(g, config)     nets.append(net)     dinos.append(klase.Dino(-55))     g.fitness = 0     ge.append(g)  clock = pygame.time.Clock() score = 0 run = True  win = pygame.display.set_mode((WIN_W, WIN_H))  while run:     clock.tick(30)     for event in pygame.event.get():         if event.type == pygame.QUIT:             run = False             pygame.quit()             quit()      cacti_ind = 0     if len(dinos) > 0:         if len(cactis) > 1 and dinos[0].x > cactis[0].x + cactis[0].img.get_width():             cacti_ind = 1     else:         run = False         break      if len(cactis) == 0:         funkcije.spawn_cacti(cactis)      for x, dino in enumerate(dinos):         ge[x].fitness += 0.01         if dino.y < dino.init_y + dino.dy:             ge[x].fitness -= 0.4         try:             next_cacti = cactis[cacti_ind + 1]         except IndexError:             next_cacti = cactis[cacti_ind]         output = nets[x].activate((dino.x, dino.y, abs(dino.x - cactis[cacti_ind].x), abs(dino.x - (cactis[cacti_ind].x + cactis[cacti_ind].img.get_width())), abs(dino.x - next_cacti.x)))         if output[0] > 0.5:             dino.jump()      add_score = False     for cacti in cactis:         for x, dino in enumerate(dinos):             if cacti.collide(dino):                 ge[x].fitness -= 1                 dinos.pop(x)                 nets.pop(x)                 ge.pop(x)              if not add_score and cacti.x + cacti.img.get_width() < dino.x:                 add_score = True      rem = []     for cacti in cactis:         if cacti.x + cacti.img.get_width() < 0:             rem.append(cacti)      for r in rem:         cactis.remove(r)      if add_score:         score += 1         for g in ge:             g.fitness += 5         add_score = False      funkcije.move(floor, dinos, cactis)     funkcije.draw_window(win, floor, dinos, cactis, score, gen) 

def run(config_path): config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)

p = neat.Population(config)  p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats)  winner = p.run(main, 50) 

if name == 'main': local_dir = os.path.dirname(file) config_path = os.path.join(local_dir, 'config-feedforward.txt') run(config_path)

</code> 
Add Comment

Okay so basically I’ve just added every possible folder associated with python to PYTHONPATH and now it does accept pygame(It doesn’t give any errors when I import pygame from console), but now it won’t run programs at all and it also doesn’t want to open the IDLE. I can still run it normally from Sublime

Add Comment

If you want to use it in PyCharm you need to go to settings then Project:"name of the project" then Project Interpreter then on the "plus" sign and find and install Pygame and/or other modules. Ofcourse first you need to set the interpreter if it is not already set.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.