为什么当我使用子进程中的call()运行python文件时出现ModuleNotFoundError

时间:2019-12-14 18:52:10

标签: python pygame python-3.8

所以我正在制作一个飞扬的鸟游戏,每次玩家死亡时,我关闭程序并再次运行它:

call(["python", "app.py"])
quit(1)

每运行call(["python", "app.py"]),我就会得到ModuleNotFoundError: No module named 'pygame' 即使我已经安装了pygame。

app.py:

import pygame as py
from subprocess import call



# Initialize Pygame
py.init()
# Window
icon = py.image.load(('flappyicon.jpg'))
window = py.display.set_mode((800, 600))
window.fill((155, 152, 13))
py.display.set_caption('Flappy Bird')
py.display.set_icon(icon)

# Player
playerImg = py.image.load('flappy.png')
playerImg = py.transform.scale(playerImg, (45, 45))
playerY = 250
playerX = 30
# Player Physics
playerYDownVelocity = 0


def player(playerX, playerY):
    window.blit(playerImg, (playerX, playerY))


# Game Loop
lost = False
running = True
while running:
    window.fill((155, 152, 13))
    for event in py.event.get():
        if event.type == py.QUIT:
            running = False
        if event.type == py.KEYDOWN:
            if event.key == py.K_SPACE:
                playerYDownVelocity = -0.4

            if event.key == py.K_r:
                if lost == True:
                    print('elooor')
                    rerun = 'app.py'
                    call(["python", "app.py"])
                    quit(1)

    if playerY >= 550:
        # gameOver()
        lost = True
        playerY = 550
    elif playerY <= 0:
        playerY = 0

    # Physics
    playerYDownVelocity += 0.001
    playerY += playerYDownVelocity
    player(playerX, playerY)
    py.display.update()

当我从pycharm运行它时,它可以工作。我用谷歌搜索,但找不到任何东西。

1 个答案:

答案 0 :(得分:0)

好吧,两件事

  
      
  1. 首先,如果要在虚拟环境中制作游戏(并且绝对应该确保pygame与该虚拟环境一起安装,否则,它将仅全局安装在系统中,而不是虚拟环境中,并且这是该错误的最可能原因

  2.   
  3. 二,您应该考虑使用Popen()而不是call(),但这部分只是一个友好的建议

  4.