import subprocess try: subprocess.run(["python", "-c", r'input("What is the answer?\n")'], timeout=3) except subprocess.TimeoutExpired: print("\nOut of time")