如何使 python 脚本在 Windows 上可执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4235834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:52:49  来源:igfitidea点击:

How to make python scripts executable on Windows?

pythonwindowsshebang

提问by Hamish Grubijan

Possible Duplicate:
Set up Python on Windows to not type python in cmd

可能的重复:
在 Windows 上设置 Python 以不在 cmd 中键入 python

When I use python on Linux, or even Mac OS from command line, I take advantage of the shebang and run some of my scripts directly, like so: ./myScript.py. I do need to give this script executable permissions, but that is all.

当我使用Python在Linux上,甚至的Mac OS命令行,我趁家当,并直接运行一些我的脚本,像这样:./myScript.py。我确实需要授予此脚本可执行权限,但仅此而已。

Now, I just installed Python 3.1.2 on Windows 7, and I want to be able to do the same from command line. What additional steps do I need to follow?

现在,我刚刚在 Windows 7 上安装了 Python 3.1.2,我希望能够从命令行执行相同的操作。我需要遵循哪些额外步骤?

采纳答案by digitalfoo

This sums it up better than I can say it:

这总结得比我说的要好:

http://docs.python.org/faq/windows.html

http://docs.python.org/faq/windows.html

More specifically, check out the 2nd section titled "How do I make Python scripts executable?"

更具体地说,请查看标题为“如何使 Python 脚本可执行?”的第二部分。

On Windows, the standard Python installer already associates the .pyextension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as foo.py. If you'd rather be able to execute the script by simple typing foowith no extension you need to add .pyto the PATHEXTenvironment variable.

在 Windows 上,标准 Python 安装程序已经将.py扩展名与文件类型 ( Python.File)相关联,并为该文件类型提供了一个运行解释器 ( D:\Program Files\Python\python.exe "%1" %*)的打开命令。这足以使脚本从命令提示符可执行为foo.py. 如果您希望能够通过简单的输入foo而没有扩展名来执行脚本,您需要添加.pyPATHEXT环境变量中。