>>importpy2exe>>>" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python腳本文件打包成可執行文件的方法

系統 2061 0

將Python腳本文件包裝成可執行文件,其目的有二:

一則: 不需要依賴Python編譯器就可以運行軟件
二則: 不想讓自己的源碼公布出去

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

安裝py2exe
安裝該工具很簡單:
只需要從官方網站:http://www.py2exe.org/下載與版本對應的安裝程序,點擊下一步即可完成安裝。
安裝后,執行import py2exe,不報錯則表示安裝成功!

復制代碼 代碼如下:

>>> import py2exe?
>>>?

NOTE: 目前該工具只支持到Python2.7, 對于Python3而言,必須借助另外一個工具:cx_freeze

使用py2exe

第一步: 準備源代碼,假如名為:Hello.py

Python腳本文件打包成可執行文件的方法_第1張圖片


第二步: 準備編譯腳本,假如名為:setup.py

復制代碼 代碼如下:

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

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

復制代碼 代碼如下:

D:\temp>setup.py py2exe

Python腳本文件打包成可執行文件的方法_第2張圖片

運行之后,會在我當前運行的目錄下(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",??????? # 名字信息?
)?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 黔西| 南通市| 漾濞| 奇台县| 罗山县| 宝鸡市| 浑源县| 大化| 精河县| 云霄县| 承德市| 综艺| 西林县| 卓尼县| 古田县| 井陉县| 大新县| 彭水| 日照市| 阿克苏市| 金乡县| 顺义区| 绵阳市| 晋州市| 敦化市| 嘉兴市| 武汉市| 饶平县| 瑞丽市| 错那县| 开阳县| 民丰县| 乐东| 阆中市| 秭归县| 肥乡县| 榆中县| 伊宁市| 利辛县| 丰镇市| 景洪市|