如何从 windows 命令行 运行 python 文件 (.py) 而不必先键入 python?

How to run a python file (.py) from the windows command-line without having to type python first?

假设我有一个名为 file.py 的 python 文件。 通常从命令行 运行 这个文件我会做:

python path\to\file\file.py

我的问题是,是否可以在文件路径前没有 python 的情况下执行此操作,如下所示:

path\to\file\file.py

或者,如果我的环境变量中有 file.py 的路径,只需:

file.py

我想值得注意的是,我想使用将接受命令行参数的 python 文件来执行此操作。谢谢:)

您面临的问题是您的 python 应用程序实际上不是应用程序。它是一个解释脚本。这是因为 Python 是 Interpreted Language.

这类似于您拥有 Word 或 Excel 文档。它们分别由它们的应用程序解释:Word 和 Excel。操作系统知道使用哪个应用程序来使用已注册的关联程序来解释它们。

官方 Python 常见问题解答在此处对此进行了解释: https://docs.python.org/2/faq/windows.html#how-do-i-make-python-scripts-executable