99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python腳本文件打包成可執(zhí)行文件的方法
Python腳本文件打包成可執(zhí)行文件的方法
2018-02-28
收藏

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,不報錯則表示安裝成功!

復(fù)制代碼代碼如下:

>>> import py2exe 
>>> 

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

使用py2exe

第一步: 準(zhǔn)備源代碼,假如名為:Hello.py


第二步: 準(zhǔn)備編譯腳本,假如名為:setup.py

復(fù)制代碼代碼如下:

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

第三步: 運(yùn)行命令: setup.py py2exe

復(fù)制代碼代碼如下:

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獲取幫助信息

復(fù)制代碼代碼如下:

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ì)的編譯腳本

復(fù)制代碼代碼如下:


# -*- 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,               # 是否需要可選項(xiàng),默認(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ù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }