Python腳本文件打包成可執(zhí)行文件的方法
將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", # 名字信息
)
相關(guān)文章
基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲實(shí)例
這篇文章主要介紹了基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲,實(shí)例分析了Python實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04OpenCV實(shí)現(xiàn)從灰度圖像切出Mask前景區(qū)域
本文主要介紹了如何利用OpenCV實(shí)現(xiàn)從灰度圖像,根據(jù)閾值,切出多個前景區(qū)域,過濾面積太小的圖像。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06python?動態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新的方法
這篇文章主要介紹了python?動態(tài)導(dǎo)入模塊,實(shí)現(xiàn)模塊熱更新,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08Pycharm中import?torch報錯,python中import?torch不報錯的解決
這篇文章主要介紹了Pycharm中import?torch報錯,python中import?torch不報錯的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01python編程實(shí)現(xiàn)清理微信重復(fù)緩存文件
這篇文章主要為大家介紹了使用python編程來實(shí)現(xiàn)清理微信重復(fù)緩存文件的示例代碼過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11Pytorch中的 torch.distributions庫詳解
這篇文章主要介紹了Pytorch中的 torch.distributions庫,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02解決python3報錯之takes?1?positional?argument?but?2?were?gi
這篇文章主要介紹了解決python3報錯之takes?1?positional?argument?but?2?were?given問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03