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

Pyinstaller打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié)

 更新時(shí)間:2019年04月02日 15:52:14   作者:圈圈烴  
今天小編就為大家分享一篇關(guān)于Pyinstaller打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

Pyinstaller 打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié)

簡介

有時(shí)候自己寫了個(gè)python腳本覺得挺好用想要分享給小伙伴,但是每次都要幫他們的電腦裝個(gè)python環(huán)境。雖然說裝一下也快,但是相對(duì)來說效率還是不高,要是能將python的**.py文件轉(zhuǎn)化為.exe**,那么世界將變得更美好。這篇文章我將簡單的介紹如何使用Pyinstaller來打包我們的python腳本。

安裝 Pyinstaller

pyinstaller的官網(wǎng)為:http://www.pyinstaller.org/

如果有什么問題一般都能夠在這里找到解答。

安裝 pyinstaller還是非常的方便的,只需要一句話:

pip install pyinstaller

就OK了

打包 Python腳本

關(guān)于打包其實(shí)也很簡單,也只需要一句話:

pyinstaller yourprogram.py

當(dāng)然pyinstaller命令的語法是:

pyinstaller [options] script [script ...] | spec文件

在運(yùn)行 pyinstaller命令后在yourprogram.py同一個(gè)目錄下會(huì)產(chǎn)生一個(gè)文件兩個(gè)文件夾:

  1. yourprogram.spec文件,該文件中寫了一些配置,可以打開進(jìn)行修改,下一次可以直接打包該文件,不用打包yourprogram.py了。
  2. build文件夾,包含一些日志文件和工作文件
  3. dist文件夾,包含可執(zhí)行文件

1. 控制臺(tái)窗口選項(xiàng)

-c, --console, --nowindowed 打包程序運(yùn)行時(shí),打開標(biāo)準(zhǔn)i / o控制臺(tái)窗口(默認(rèn)) 
-w, --windowed, --noconsole 打包程序運(yùn)行時(shí),不打開標(biāo)準(zhǔn)i / o控制臺(tái)窗口(默認(rèn)) 

2. 軟件圖標(biāo)選項(xiàng)

-i FILE.ico , --icon FILE.ico 為你的程序添加一個(gè)圖標(biāo)

3. 版本信息選項(xiàng)

--version-file FILE.txt 可以將您的版本信息添加到你的軟件當(dāng)中

這個(gè)FILE.txt的文件內(nèi)容為:

# UTF-8
#
VSVersionInfo(
 ffi=FixedFileInfo(
  # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
  # Set not needed items to zero 0.
  filevers=(10, 0, 17134, 1),
  prodvers=(10, 0, 17134, 1),
  # Contains a bitmask that specifies the valid bits 'flags'r
  mask=0x3f,
  # Contains a bitmask that specifies the Boolean attributes of the file.
  flags=0x0,
  # The operating system for which this file was designed.
  # 0x4 - NT and there is no need to change it.
  OS=0x40004,
  # The general type of file.
  # 0x1 - the file is an application.
  fileType=0x1,
  # The function of the file.
  # 0x0 - the function is not defined for this fileType
  subtype=0x0,
  # Creation date and time stamp.
  date=(0, 0)
  ),
 kids=[
  StringFileInfo(
   [
   StringTable(
    u'040904B0',
    [StringStruct(u'CompanyName', u'Microsoft Corporation'),
    StringStruct(u'FileDescription', u'Windows Command Processor'),
    StringStruct(u'FileVersion', u'10.0.17134.1 (WinBuild.160101.0800)'),
    StringStruct(u'InternalName', u'cmd'),
    StringStruct(u'LegalCopyright', u'© Microsoft Corporation. All rights reserved.'),
    StringStruct(u'OriginalFilename', u'Cmd.Exe'),
    StringStruct(u'ProductName', u'Microsoft® Windows® Operating System'),
    StringStruct(u'ProductVersion', u'10.0.17134.1')])
   ]),
  VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
 ]
)

4. 生成結(jié)果選項(xiàng)

-D,--onedir 這會(huì)創(chuàng)建一個(gè)包含可執(zhí)行文件的單文件夾包(默認(rèn))
-F,--onefile 這個(gè)只會(huì)創(chuàng)建一個(gè)可執(zhí)行文件
--specpath DIR 用于存儲(chǔ)生成的spec文件的文件夾(默認(rèn)值:當(dāng)前目錄)
-n NAME, --name NAME 應(yīng)用程序的名稱(默認(rèn)值:第一個(gè)腳本的名稱)

以上四個(gè)就是常用的參數(shù),可以根據(jù)自己的需求進(jìn)行設(shè)置。

例子

下面這個(gè)是我打包一個(gè)“自動(dòng)更新桌面的腳本”的命令:

G:\PyinstallerTest\Auto_Wallpaper>pyinstaller -F -i G:\PyinstallerTest\Auto_Wallpaper\icon\moon.ico --version-file=version.txt Auto_Wallpaper_of_Moon.py

執(zhí)行后就能夠成功的將**.py腳本轉(zhuǎn)化為.exe**可執(zhí)行文件。

錯(cuò)誤總結(jié)

在這里我會(huì)總結(jié)我在使用pyinstaller過程中出現(xiàn)的錯(cuò)誤和解決方法

1. AttributeError: ‘str' object has no attribute ‘items'

錯(cuò)誤內(nèi)容截?。?/p>

for real_module_name, six_module_name in real_to_six_module_name.items():
AttributeError: 'str' object has no attribute 'items'

解決方法:

你需要更新一下你的setuptools

pip install --upgrade setuptools

2. 打包后運(yùn)行.exe程序調(diào)試閃退

有時(shí)候?qū)?py文件打包成.exe文件后,不一定能夠正常運(yùn)行。但是點(diǎn).exe后程序一閃而退,就不知道問題出在哪里。

解決方法:

在cmd行里面運(yùn)行你的.exe文件

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論