pythonrunpy

Can't run more than 2 paths with runpy


I have this code, the jogo==1 and jogo==2 options work fine and open what I want but the rest of the options dont open the .py files, is anything wrong with my code?

import runpy

def menuPrincipal():

  print("\nMenu\n")

  print("1 - Jogo do Galo")
  print("2 - 4 em linha")
  print("3 - Jogo da gloria")
  print("4 - Jogo da força")
  print("5 - Jogo Minas")
  print("6 - Sair")


while True:
  menuPrincipal()
  jogo = int(input("Escolha um jogo: "))

  if jogo == 1:
     runpy.run_path(path_name='jogoDoGalo.py')
  elif jogo == 2:
       runpy.run_path(path_name='jogo4emLinha.py')
  elif jogo == 3:
       runpy.run_path(path_name='jogoDaGloria.py')
  elif jogo == 4:
     runpy.run_path(path_name='jogoDaForca.py')
  elif jogo == 5:
       runpy.run_path(path_name='jogoMinas.py')
  elif jogo == 6:
       break

Solution

  • This is a mere guess, but there might be code in those files that don't seem to run inside an if __name__ == "__main__": condition.

    If that's the case, that code won't be executed when called via runpy because then those files will load into Python under module names different than "__main__". Try removing the if __name__ == "__main__": condition.