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

 更新时间:2015年06月02日 17:28:17   投稿:junjie  
这篇文章主要介绍了Python脚本文件打包成可执行文件的方法,本主要讲解了Python2.X版本的打包方法,对Python3.X的打包也有简单介绍,需要的朋友可以参考下

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

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

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

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

复制代码 代码如下:

>>> import py2exe 
>>> 

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

使用py2exe

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


第二步: 准备编译脚本,假如名为:setup.py

复制代码 代码如下:

from distutils.core import setup 
import py2exe 
 
setup(windows=['Hello.py']) 

第三步: 运行命令: setup.py py2exe

复制代码 代码如下:

D:\temp>setup.py py2exe

运行之后,会在我当前运行的目录下(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 

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控制端口  
    windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}], # 针对GUI图形窗口 
    data_files=[("magic",["App_x86.exe",]),], 
    version = "v1.01",             # 版本信息 
    description = "py2exe testing",# 描述信息  
    name = "Hello, Py2exe",        # 名字信息 

相关文章

  • 基于Python实现的百度贴吧网络爬虫实例

    基于Python实现的百度贴吧网络爬虫实例

    这篇文章主要介绍了基于Python实现的百度贴吧网络爬虫,实例分析了Python实现网络爬虫的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • OpenCV实现从灰度图像切出Mask前景区域

    OpenCV实现从灰度图像切出Mask前景区域

    本文主要介绍了如何利用OpenCV实现从灰度图像,根据阈值,切出多个前景区域,过滤面积太小的图像。文中的示例代码讲解详细,需要的可以参考一下
    2022-06-06
  • python安装pywifi全过程

    python安装pywifi全过程

    这篇文章主要介绍了python安装pywifi全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • python 动态导入模块实现模块热更新的方法

    python 动态导入模块实现模块热更新的方法

    这篇文章主要介绍了python 动态导入模块,实现模块热更新,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • Pycharm中import torch报错,python中import torch不报错的解决

    Pycharm中import torch报错,python中import torch不报错的解决

    这篇文章主要介绍了Pycharm中import torch报错,python中import torch不报错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • pandas如何快速去除列名中的特殊符号

    pandas如何快速去除列名中的特殊符号

    在使用Pandas处理数据时,经常需要处理数据中的列名column name,有时候,列名可能包含特殊字符,比如空格、点号、括号等,这些特殊字符可能会导致下一步的代码出错,因此需要将这些特殊字符从列名中删除,下面先介绍pandas如何去除列名中的特殊符号,感兴趣的朋友一起看看吧
    2024-01-01
  • python编程实现清理微信重复缓存文件

    python编程实现清理微信重复缓存文件

    这篇文章主要为大家介绍了使用python编程来实现清理微信重复缓存文件的示例代码过程,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-11-11
  • Pytorch中的 torch.distributions库详解

    Pytorch中的 torch.distributions库详解

    这篇文章主要介绍了Pytorch中的 torch.distributions库,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Python代码中如何读取键盘录入的值

    Python代码中如何读取键盘录入的值

    在本篇文章里小编给大家分享的是关于Python代码中读取键盘录入值的方法,需要的朋友们可以参考下。
    2020-05-05
  • 解决python3报错之takes 1 positional argument but 2 were given问题

    解决python3报错之takes 1 positional argument but 2 were gi

    这篇文章主要介绍了解决python3报错之takes 1 positional argument but 2 were given问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03

最新评论