在 Windows 中运行没有“python"的 python 程序 [英] Run python program without 'python' in Windows

查看:48
本文介绍了在 Windows 中运行没有“python"的 python 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

#!C:\Python27\python.exe

打印Hello World"

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

我还参考了如何使 Python 脚本可执行?"网站部分

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

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

感谢您的回复以及反对票.

解决方案

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

按照以下步骤操作:

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

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

from distutils.core 导入设置导入py2exe设置(控制台 = ['test.py'])

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

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

希望对你有帮助..!!

谢谢

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 :

#!C:\Python27\python.exe

print "Hello World"

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).

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.

Thanks.

Update :

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

This is what I followed (image below)

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

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

Thanks for the responses as well as the downvotes.

解决方案

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

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.

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

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.

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.

Hope it helps you..!!

Thanks

这篇关于在 Windows 中运行没有“python"的 python 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆