Python 函数不是来自 cmd 的 运行,路径变量已定义且 python 控制台正常运行

Python function not running from cmd, path variables are defined and python console is functional

  1. 我在 Windows 10 机器上使用 python 3.6.8。我已将 python 添加到 PATH 环境变量,并且 python 控制台可通过命令提示符运行。

  2. 我编写了一个简单的 hello world 脚本,使用 pyinstaller 将其转换为 exe。这很好用。

  3. 问题是当我将相同的代码包装在 main() 方法(下面的代码)中,然后创建 exe 文件时,cmd 打开和关闭的速度非常快。

我首先尝试使用 python 3.7.1 和 (2) - (3) 如上所述根本不起作用。然后我将我的 python 版本降级到 python 3.6.8 并且 (2) 已解决,但是 (3) 仍然是一个问题。代码 (3) 可以使用 python -c "import main; print(main.main())" 通过 Windows 命令提示符执行,因此代码本身没有问题。

(2) 的源代码,如上定义 (hello.py)

print("Hello World")
input("Press any key to exit")

(3) (main.py) 的源代码:

def main():
  print("Hello World")
  input("Press any key to exit")

使用 PyInstaller 创建 (2):

使用 PyInstaller 创建 (3) (main.py):

我的问题有解决办法吗?我已经编写了一个小游戏作为一个项目,我想为 Windows 制作该游戏的可执行文件(1.0.0 版),用户不需要安装 python。

您实际上并没有在代码中调用 main()。 将此添加到底部。

def main():
  print("Hello World")
  input("Press any key to exit")

if __name__== "__main__":
  main()