如何修复错误:“没有名为‘pygame’的模块”

问题描述 投票:0回答:9

我安装了 pygame 模块,当我尝试从 PyCharm 或 Sublime Text 运行它时,它工作得很好,但是当我尝试从控制台或 IDLE 运行它时,它显示:“错误:没有名为 'pygame' 的模块”。我应该提到我的 python 3.8 没有安装在它的默认位置,而是安装在自定义文件夹中的其他分区上。当我第一次安装 python 时,我也遇到了 PATH 和 python 的问题。

python python-3.x path pygame modulenotfounderror
9个回答
1
投票

您必须将该模块安装在最新 pythom 版本位置的 Scripts 文件夹中。 我的意思是:

  1. 打开命令提示符。

  2. 找到安装 python 的文件夹。为此,请输入:

    cd /d [python安装路径]

例如:

cd /d E:\Python\Scripts

注意:您必须在输入 python 位置后添加 \Scripts。 3) 类型:

pip install pygame
  1. 运行后,您可以打开位于insidepython文件夹的空闲文件。

  2. 解决了。

如果您觉得有帮助,请投票。


0
投票

您是否在脚本开头导入了 pygame ?这可能就是问题所在。


0
投票

向我们展示您的代码,我们会尽力帮助您

另外,如果你确实在代码中导入了 pygame,请记住添加

import sys 
print sys.path

到前三行以及导入 pygame 之前:)

import sys
print sys.path
import pygame

我从这里拿了这个。


0
投票

这是我的主要代码。不知道如何格式化它......


```
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)

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)

0
投票

好吧,基本上我刚刚将与 python 相关的每个可能的文件夹添加到 PYTHONPATH 中,现在它确实接受 pygame (当我从控制台导入 pygame 时它不会给出任何错误),但现在它根本不会运行程序并且它也不想打开IDLE。我仍然可以从 Sublime 正常运行它


0
投票

如果您想在 PyCharm 中使用它,您需要转到设置,然后选择项目:“项目名称”,然后选择项目解释器,然后在“加号”上找到并安装

p = neat.Population(config)

p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)

winner = p.run(main, 50)
和/或其他模块。当然,如果尚未设置,首先您需要设置解释器。


0
投票

我曾经遇到过这个错误,但我所需要做的就是从我的控制台安装它。


0
投票

我在 VS Code 中使用 Python 时遇到了同样的问题。事实证明,我的系统上安装了多个版本的 Python,尽管我有

</code>
pygame,但它与 VS Code 中活动的 python 版本并不“一致”

要解决此问题,请在 VS Code 中,通过单击左下角并从下拉菜单中进行选择来选择正确的解释器路径。希望这可以帮助 enter image description here


0
投票

我刚刚使用 python.org 中的安装程序卸载并重新安装了 python,并确保我添加了路径。然后从 vscode 运行 pip install pygame。

© www.soinside.com 2019 - 2024. All rights reserved.