博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将Python脚本文件转化成Windows可执行exe文件
阅读量:4144 次
发布时间:2019-05-25

本文共 1046 字,大约阅读时间需要 3 分钟。

1、准备工作

 

确保机子安装有以下程序:

①Python:可以从官方网站()下载

下载快速通道(Python2.7.2 win32):

②Py2exe:可以从官方网站()下载

下载快速通道(py2exe-0.6.9.win32-py2.7):

 

2、详细步骤

控制台应用程序

①    将代码另存为.py文件(本例命名为test.py),并确保代码能够正确运行。

下面的python脚本代码是在控制台中输出数字0-9。

print "Python script to exe test program"count = 0while count < 10:    print "count = " + str(count) +"\n"    count = count + 1

可以在控制台中键入python filename(Python的安装目录要加入到系统的PATH中),回车后可以看到运行结果。

②    创建安装脚本。

from distutils.core import setupimport py2exesetup(console=['test.py'])

在命令行控制台中运行python setup.py py2exe,可以看到有以下信息。

 

这样就生成exe可执行文件,可以在子目录\dist下找到test.exe文件。运行test.exe结果如下:

 

GUI应用程序

①    创建一个名为gui.py的新文件,内容如下:

from Tkinter import *frmMain = Tk()label = Label(frmMain, text="Welcome to py2exe!")label.pack()frmMain.mainloop()

简单解释一下代码的含义:导入Tkinter包,创建一个主窗口,在该窗口中创建一个标签,该标签的文本内容是“Welcome to py2exe!”并把标签的大小设为适应文本内容大小。

在命令行下输入“python gui.py”运行效果如下:

 

②    创建安装脚本

from distutils.core import setupimport py2exesetup(console=['gui.py'])

同样,在命令行控制台中运行python setup.py py2exe,可以看到生成了两个文件夹(build和dist),可以在\dist目录中找到gui.exe文件。

本人也是初学者,有什么写得不对的,还望大虾指正。。。

 

 

 

转载请注明出处!

 

你可能感兴趣的文章
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
Commit our mod to our own repo server
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>