將Python腳本文件包裝成可執(zhí)行文件,其目的有二:
一則: 不需要依賴Python編譯器就可以運(yùn)行軟件
二則: 不想讓自己的源碼公布出去
常用的工具有: py2exe、cx_freeze等
【工具:py2exe】
安裝py2exe
安裝該工具很簡單:
只需要從官方網(wǎng)站:http://www.py2exe.org/下載與版本對應(yīng)的安裝程序,點(diǎn)擊下一步即可完成安裝。
安裝后,執(zhí)行import py2exe,不報錯則表示安裝成功!
>>> import py2exe?
>>>?
NOTE: 目前該工具只支持到Python2.7, 對于Python3而言,必須借助另外一個工具:cx_freeze
使用py2exe
第一步: 準(zhǔn)備源代碼,假如名為:Hello.py
第二步: 準(zhǔn)備編譯腳本,假如名為:setup.py
from distutils.core import setup?
import py2exe?
?
setup(windows=['Hello.py'])?
第三步: 運(yùn)行命令: setup.py py2exe
D:\temp>setup.py py2exe
運(yùn)行之后,會在我當(dāng)前運(yùn)行的目錄下(D:\temp)默認(rèn)生成dict目錄,里面的文件如下:
默認(rèn)情況下,py2exe在目錄dist下創(chuàng)建以下這些必須的文件:?
1、一個或多個exe文件。如本例為: Hello.exe?
2、python##.dll。 如本例中: Python27.dll?
3、.pyd文件,它們是已編譯的擴(kuò)展名,它們是exe文件所需要的;加上其它的.dll文件,這些.dll是.pyd所需要的。?
4、一個library.zip文件,它包含了已編譯的純的python模塊如.pyc或.pyo?
第四步: 雙擊Hello.exe可執(zhí)行文件,跟源代碼運(yùn)行后同樣的結(jié)果:
其他
1: 執(zhí)行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: 一個詳細(xì)的編譯腳本
# -*- coding: cp936 -*-?
from distutils.core import setup?
import py2exe?
?
includes = ["encodings", "encodings.*"]?
?
options = {"py2exe":???
??????????? {"compressed": 1,????? # 壓縮???
???????????? "optimize": 2,??????? # 優(yōu)化級別?
???????????? "ascii": 1,?????????? #??
???????????? "includes":includes,? # 編碼方式?
???????????? "bundle_files": 1???? # 所有文件打包成一個zipfile或exe文件,有效級別1,2,3?
??????????? }}?
setup(?
??? options=options,?????????????? # 是否需要可選項,默認(rèn)為None?
??? zipfile=None,????????????????? # 是否需要壓縮像,默認(rèn)為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",??????? # 名字信息?
)?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
