欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Pyinstaller轉(zhuǎn)換.py文件為.exe可執(zhí)行程序過程詳解

 更新時(shí)間:2019年08月06日 10:49:49   作者:BengDou_Do&Think  
這篇文章主要介紹了使用Pyinstaller轉(zhuǎn)換.py文件為.exe可執(zhí)行程序過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

 前言

pyinstaller能夠在Windows、Linux等操作系統(tǒng)下將Python腳本打包成可直接運(yùn)行程序。使Python腳本可以在沒有安裝Python的環(huán)境中直接運(yùn)行,方便共享。

開發(fā)環(huán)境

python 2.7.12 + Windows7

注意事項(xiàng)

1、待轉(zhuǎn)換的.py文件絕對(duì)路徑最好不要包含中文字符。容易出現(xiàn)一些莫名其妙的問題。

2、python中需要有.py文件中用到的第三方庫(kù)。否則在轉(zhuǎn)換后的.exe文件中會(huì)出現(xiàn)不符合預(yù)期的結(jié)果。

pyinstaller安裝步驟

1、配置pip鏡像源。pip配置方法參考pip配置和安裝第三方模塊。如果已經(jīng)配置,跳過。

2、打開cmd命令行窗口,輸入pip install pyinstaller,安裝pyinstaller庫(kù)。

C:\Users\Administrator>pip install pyinstaller
Collecting pyinstaller
 Downloading http://pypi.doubanio.com/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyIns
taller-3.3.1.tar.gz (3.5MB)
  100% |████████████████████████████████| 3.5MB 112kB/s
Requirement already satisfied: setuptools in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: pefile>=2017.8.1 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: macholib>=1.8 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: dis3 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: future in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: altgraph>=0.15 in c:\python27\lib\site-packages (from macholib>=1.8->pyinstaller)
Installing collected packages: pyinstaller
 Running setup.py install for pyinstaller ... done
Successfully installed pyinstaller-3.3.1

3、確認(rèn)pyinstaller安裝結(jié)果,位于c:\Python27\Scripts路徑下。執(zhí)行where pyinstaller查看

C:\Users>where pyinstaller
c:\Python27\Scripts\pyinstaller.exe

pyinstaller基本語(yǔ)法

pyinstaller [options] script

options常用選項(xiàng)說明:

-F,-onefile: 表示生成單個(gè)可執(zhí)行文件,常用。
-w, -windowed, -noconsole:表示去掉控制臺(tái)窗口,這在GUI界面時(shí)非常有用。不過如果是命令行程序的話那就把這個(gè)選項(xiàng)刪除吧!
-p 表示你自己自定義需要加載的類路徑,一般情況下用不到
-i 表示可執(zhí)行文件的圖標(biāo)。注意:圖片后綴必須是.ico
-c,console,-nowindowed:使用控制臺(tái),無(wú)窗口(默認(rèn))
-D,-onedir:創(chuàng)建一個(gè)目錄,包含EXE文件,但會(huì)依賴很多文件(默認(rèn)選項(xiàng))

基本實(shí)例:pyinstaller -F myscript.py。

pyinstaller更多語(yǔ)法見官網(wǎng)說明: https://pyinstaller.readthedocs.io/en/stable/usage.html

pyinstaller原理簡(jiǎn)介

pyinstaller其實(shí)就是把python解釋器和腳本打包成一個(gè)可執(zhí)行文件,和編譯成真正的機(jī)器碼是完全兩回事。所以打包不一定會(huì)提高運(yùn)行效率,可能會(huì)降低運(yùn)行效率,但是好處是在運(yùn)行者機(jī)器上不用安裝python和腳本所依賴的庫(kù)。

輸入指定的腳本后,首先pyinstaller會(huì)分析該腳本所依賴的其他依賴,然后進(jìn)行查找、復(fù)制,把所有相關(guān)的依賴都收集起來并驚醒加密處理,包括python解釋器,最后把這些文件放在一個(gè)目錄下,或者打包到一個(gè)可執(zhí)行文件。然后就可以直接運(yùn)行所生成的可執(zhí)行文件。

需要注意的是,使用pyinstaller打包生成的可執(zhí)行文件,只能再和打包機(jī)器系統(tǒng)相同的環(huán)境下運(yùn)行。32位python環(huán)境打包的程序可以運(yùn)行在32/64位windows系統(tǒng)上。64位python環(huán)境打包的程序只能運(yùn)行在64位windows系統(tǒng)上。所以如果想打包程序的話,建議使用32位python環(huán)境打包。

pyinstaller使用實(shí)例

1、確認(rèn)待轉(zhuǎn)換的.py文件可正確運(yùn)行,不存在語(yǔ)法錯(cuò)誤。如ccc.py

2、執(zhí)行pyinstaller -F ${Python腳本名}完成文件轉(zhuǎn)換。.exe文件生成的絕對(duì)路徑會(huì)在倒數(shù)第二行顯示,通常位于當(dāng)前目錄下dist所在目錄下。轉(zhuǎn)換后的.exe文件名與python文件名相同。如下圖所示

d:\Program Files\Notepad++>pyinstaller -F ccc.py
INFO: PyInstaller: 3.3.1
INFO: Python: 2.7.12
INFO: Platform: Windows-7-6.1.7601-SP1
.......
INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940)
INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
INFO: checking EXE
INFO: Building EXE because out00-EXE.toc is non existent
INFO: Building EXE from out00-EXE.toc
INFO: Appending archive to EXE d:\Program Files\Notepad++\dist\ccc.exe
INFO: Building EXE from out00-EXE.toc completed successfully.

FAQs

1、如果Python腳本使用到了第三方庫(kù),如何打包?

方法一:將第三方庫(kù)對(duì)應(yīng)的包復(fù)制到待打包python腳本的同目錄下,再執(zhí)行打包命令。

方法二:pyinstaller.exe -F 路徑\文件名.py 路徑\文件名.py

2、我的python腳本主要是命令行輸出,但是程序執(zhí)行完就退出無(wú)法查看相關(guān)信息,如何處理?

在python腳本最后一行添加命令:os.system('pause') 或者 raw_input('Press enter any key to exit...')

3、 我想給我的打包后的執(zhí)行程序換個(gè)圖標(biāo),如何處理?

使用參數(shù)-i。如命令:pyinstaller -F -i tupian\qq.ico ccc.py。文件后綴名必須是.ico

4、程序運(yùn)行出現(xiàn)CMD窗口,如何去除?

帶上參數(shù)-w即可。pyinstaller.exe -F call_login.py -w (-w表示去掉控制臺(tái)窗口顯示)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論