• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python脚本文件打包成可执行文件的方法

Python脚本文件打包成可执行文件的方法

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python 可执行文件,python生成可执行文件,可执行文件,可执行文件的扩展名,steam缺少可执行文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

将Python脚本文件包装成可执行文件,其目的有二:

一则: 不需要依赖Python编译器就可以运行软件
二则: 不想让自己的源码公布出去

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

安装py2exe
安装该工具很简单:
只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装。
安装后,执行import py2exe,不报错则表示安装成功!
>>> import py2exe 
>>> 
</div>

NOTE: 目前该工具只支持到Python2.7, 对于Python3而言,必须借助另外一个工具:cx_freeze

使用py2exe

第一步: 准备源代码,假如名为:Hello.py


第二步: 准备编译脚本,假如名为:setup.py
from distutils.core import setup 
import py2exe 
 
setup(windows=['Hello.py']) 
</div>

第三步: 运行命令: setup.py py2exe
D:\temp>setup.py py2exe
</div>

运行之后,会在我当前运行的目录下(D:\temp)默认生成dict目录,里面的文件如下:

默认情况下,py2exe在目录dist下创建以下这些必须的文件: 
1、一个或多个exe文件。如本例为: Hello.exe 
2、python##.dll。 如本例中: Python27.dll 
3、.pyd文件,它们是已编译的扩展名,它们是exe文件所需要的;加上其它的.dll文件,这些.dll是.pyd所需要的。 
4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo 

第四步: 双击Hello.exe可执行文件,跟源代码运行后同样的结果:

其他

1: 执行setup.py --help获取帮助信息
Global options: 
  --verbose (-v)  run verbosely (default) 
  --quiet (-q)    run quietly (turns verbosity off) 
  --dry-run (-n)  don't actually do anything 
  --help (-h)     show detailed help message 
  --no-user-cfg   ignore pydistutils.cfg in your home directory 
 
Options for 'py2exe' command: 
  --optimize (-O)       optimization level: -O1 for "python -O", -O2 for 
                        "python -OO", and -O0 to disable [default: -O0] 
  --dist-dir (-d)       directory to put final built distributions in (default 
                        is dist) 
  --excludes (-e)       comma-separated list of modules to exclude 
  --dll-excludes        comma-separated list of DLLs to exclude 
  --ignores             comma-separated list of modules to ignore if they are 
                        not found 
  --includes (-i)       comma-separated list of modules to include 
  --packages (-p)       comma-separated list of packages to include 
  --compressed (-c)     create a compressed zipfile 
  --xref (-x)           create and show a module cross reference 
  --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels 
                        are 1, 2, or 3 (default) 
  --skip-archive        do not place Python bytecode files in an archive, put 
                        them directly in the file system 
  --ascii (-a)          do not automatically include encodings and codecs 
  --custom-boot-script  Python file that will be run when setting up the 
                        runtime environment 
 
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 
   or: setup.py --help [cmd1 cmd2 ...] 
   or: setup.py --help-commands 
   or: setup.py cmd --help 
</div>

2: 一个详细的编译脚本
# -*- coding: cp936 -*- 
from distutils.core import setup 
import py2exe 
 
includes = ["encodings", "encodings.*"] 
 
options = {"py2exe":   
            {"compressed": 1,      # 压缩   
             "optimize": 2,        # 优化级别 
             "ascii": 1,           #  
             "includes":includes,  # 编码方式 
             "bundle_files": 1     # 所有文件打包成一个zipfile或exe文件,有效级别1,2,3 
            }} 
setup( 
    options=options,               # 是否需要可选项,默认为None 
    zipfile=None,                  # 是否需要压缩像,默认为None 
    console=[{"script": "HelloCmd.py", "icon_resources": [(1, "pc.ico")]}], # 针对CMD控制端口  
&nb

您可能想查找下面的文章:

  • Python中.py文件打包成exe可执行文件详解
  • Python中.py文件打包成exe可执行文件详解
  • Python打包可执行文件的方法详解
  • Python脚本文件打包成可执行文件的方法

相关文章

  • 使用Python编写一个在Linux下实现截图分享的脚本的教程
  • 使用Python对Excel进行读写操作
  • Python编程实现删除VC临时文件及Debug目录的方法
  • Windows下安装python MySQLdb遇到的问题及解决方法
  • Python实现生成简单的Makefile文件代码示例
  • Python实现网站文件的全备份和差异备份
  • 在Windows系统上搭建Nginx+Python+MySQL环境的教程
  • Python translator使用实例
  • Python的迭代器和生成器使用实例
  • 利用Python开发实现简单的记事本

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python使用multiprocessing模块实现带回调函数的异步调用方法
    • python简单实现旋转图片的方法
    • Python发送form-data请求及拼接form-data内容的方法
    • Python入门及进阶笔记 Python 内置函数小结
    • tensorflow estimator 使用hook实现finetune方式
    • Python中装饰器的一个妙用
    • 解析Python中的__getitem__专有方法
    • Python函数式编程指南(四):生成器详解
    • Python爬虫代理IP池实现方法
    • python学习笔记之调用eval函数出现invalid syntax错误问题

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有