在 Windows 中运行没有“python”的 python 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37828739/
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-19 19:58:45  来源:igfitidea点击:

Run python program without 'python' in Windows

pythonpython-2.7

提问by niladri chakrabarty

I have written a simple python script, and I need to run it as an executable, i.e., without the 'python' word at the beginning of the program. The script (simple_prog.py) is :

我编写了一个简单的python 脚本,我需要将它作为可执行文件运行,即在程序开头没有'python' 字样。脚本(simple_prog.py)是:

#!C:\Python27\python.exe

print "Hello World"

#!C:\Python27\python.exe

打印“你好世界”

Whenever I am running the script as 'python simple_prog.py', it is printing the output alright, but without that, it is printing nothing (image below).

每当我将脚本作为“python simple_prog.py”运行时,它都会正常打印输出,但没有它,它什么也不打印(下图)。

image

图片

I have also referred to the "How do I make Python scripts executable?" section in the site https://docs.python.org/3/faq/windows.html#id3, from the stackoverflow question How to make python scripts executable on Windows?, but could not understand the solution.

我还提到了“如何使 Python 脚本可执行?” 站点https://docs.python.org/3/faq/windows.html#id3中的部分,来自 stackoverflow 问题如何使 python 脚本在 Windows 上可执行?,但无法理解解决方案。

Thanks.

谢谢。

Update :

更新 :

Solved the problem from the stackoverflow link : Set up Python on Windows to not type python in cmd

从stackoverflow链接解决了问题:Setup Python on Windows to not type python in cmd

This is what I followed (image below)

这就是我遵循的(下图)

enter image description here

在此处输入图片说明

Please note the position of double-quotes in the ftype command.

请注意 ftype 命令中双引号的位置。

The second command (simple_prog) ran successfully because I updated the PATHEXT variable by adding ".PY" in it.

第二个命令 (simple_prog) 成功运行,因为我通过在其中添加“.PY”来更新 PATHEXT 变量。

Thanks for the responses as well as the downvotes.

感谢您的回复以及downvotes。

回答by CSMaverick

Prerequisites:Install py2exe v0.6.9 which is compatible with python 2.7

先决条件:安装与python 2.7兼容的py2exe v0.6.9

Follow below Steps:

请按照以下步骤操作:

1.Save your code in to test.py (or any name with .py extension) file. Make sure that the code works fine by running it using python.

1.将您的代码保存到 test.py(或任何带有 .py 扩展名的名称)文件中。通过使用 python 运行代码来确保代码工作正常。

2.Then create a new file with name setup.py and paste the following code in to it.

2.然后创建一个名为 setup.py 的新文件并将以下代码粘贴到其中。

from distutils.core import setup
import py2exe
setup(console=['test.py'])

3.Now it is time to run the script and create the executable. To build the executable, run "python setup.py py2exe" on the command prompt.

3.现在是运行脚本并创建可执行文件的时候了。要构建可执行文件,请在命令提示符下运行“python setup.py py2exe”。

4.After Building the executable is finished. Now you can find test.exe in the \dist sub folder. Move to dist sub folder and run test.exe, you can see output in console.

4.构建可执行文件完成后。现在您可以在 \dist 子文件夹中找到 test.exe。移动到 dist 子文件夹并运行 test.exe,您可以在控制台中看到输出。

Hope it helps you..!!

希望能帮到你..!!

Thanks

谢谢

回答by Melebius

You should associate.pyfiles to be run in Python. Of course, this approach requires Python to be installed, unlike converting to .exe.

您应该关联.py要在 Python 中运行的文件。当然,这种方式需要安装 Python,不像转换为.exe.

回答by Tino A.

I believe this is the solution you are looking for. http://www.py2exe.org/

我相信这是您正在寻找的解决方案。 http://www.py2exe.org/

It basically does what it says on the lid. You can create a windows executable from your python script following the tutorial on their page.

它基本上按照盖子上的说明进行操作。您可以按照其页面上的教程从您的 python 脚本创建一个 Windows 可执行文件。