歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 將Python腳本文件包裝成可執行文件

將Python腳本文件包裝成可執行文件

日期:2017/3/1 9:32:37   编辑:Linux編程

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

一則: 不需要依賴Python編譯器就可以運行軟件

二則: 不想讓自己的源碼公布出去

常用的工具有: py2exe、cx_freeze等

工具:py2exe

  • 安裝py2exe

安裝該工具很簡單:

只需要從官方網站:http://www.py2exe.org/下載與版本對應的安裝程序,點擊下一步即可完成安裝。

安裝後,執行import py2exe,不報錯則表示安裝成功!

  1. >>> import py2exe
  2. >>>
>>> import py2exe
>>>

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

  • 使用py2exe

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

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

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

setup(windows=['Hello.py'])

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

  1. D:\temp>setup.py py2exe
D:\temp>setup.py py2exe
運行之後,會在我當前運行的目錄下(D:\temp)默認生成dict目錄,裡面的文件如下:

  1. 默認情況下,py2exe在目錄dist下創建以下這些必須的文件:
  2. 1、一個或多個exe文件。如本例為: Hello.exe
  3. 2、python##.dll。 如本例中: Python27.dll
  4. 3、.pyd文件,它們是已編譯的擴展名,它們是exe文件所需要的;加上其它的.dll文件,這些.dll是.pyd所需要的。
  5. 4、一個library.zip文件,它包含了已編譯的純的python模塊如.pyc或.pyo
默認情況下,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獲取幫助信息

  1. Global options:
  2. --verbose (-v) run verbosely (default)
  3. --quiet (-q) run quietly (turns verbosity off)
  4. --dry-run (-n) don't actually do anything
  5. --help (-h) show detailed help message
  6. --no-user-cfg ignore pydistutils.cfg in your home directory
  7. Options for 'py2exe' command:
  8. --optimize (-O) optimization level: -O1 for "python -O", -O2 for
  9. "python -OO", and -O0 to disable [default: -O0]
  10. --dist-dir (-d) directory to put final built distributions in (default
  11. is dist)
  12. --excludes (-e) comma-separated list of modules to exclude
  13. --dll-excludes comma-separated list of DLLs to exclude
  14. --ignores comma-separated list of modules to ignore if they are
  15. not found
  16. --includes (-i) comma-separated list of modules to include
  17. --packages (-p) comma-separated list of packages to include
  18. --compressed (-c) create a compressed zipfile
  19. --xref (-x) create and show a module cross reference
  20. --bundle-files (-b) bundle dlls in the zipfile or the exe. Valid levels
  21. are 1, 2, or 3 (default)
  22. --skip-archive do not place Python bytecode files in an archive, put
  23. them directly in the file system
  24. --ascii (-a) do not automatically include encodings and codecs
  25. --custom-boot-script Python file that will be run when setting up the
  26. runtime environment
  27. usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
  28. or: setup.py --help [cmd1 cmd2 ...]
  29. or: setup.py --help-commands
  30. or: setup.py cmd --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: 一個詳細的編譯腳本
  1. # -*- coding: cp936 -*-
  2. from distutils.core import setup
  3. import py2exe
  4. includes = ["encodings", "encodings.*"]
  5. options = {"py2exe":
  6. {"compressed": 1, # 壓縮
  7. "optimize": 2, # 優化級別
  8. "ascii": 1, #
  9. "includes":includes, # 編碼方式
  10. "bundle_files": 1 # 所有文件打包成一個zipfile或exe文件,有效級別1,2,3
  11. }}
  12. setup(
  13. options=options, # 是否需要可選項,默認為None
  14. zipfile=None, # 是否需要壓縮像,默認為None
  15. console=[{"script": "HelloCmd.py", "icon_resources": [(1, "pc.ico")]}], # 針對CMD控制端口
  16. windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}], # 針對GUI圖形窗口
  17. data_files=[("magic",["App_x86.exe",]),],
  18. version = "v1.01", # 版本信息
  19. description = "py2exe testing",# 描述信息
  20. name = "Hello, Py2exe", # 名字信息
  21. )
# -*- 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",        # 名字信息
)
詳情,請參考官方文檔:

http://www.py2exe.org/

--------------------------------------分割線 --------------------------------------

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved