为什么当我从子进程使用 call() 运行 python 文件时,我得到 ModuleNotFoundError



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

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. 首先,如果您在虚拟环境中制作此游戏(您绝对应该确保 pygame 安装在该虚拟环境中,否则它只是全局安装在您的系统中而不是在您的虚拟环境中,这是该错误最可能的原因

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

相关内容

  • 没有找到相关文章

最新更新