python打包工具报错_Pyinstaller打包.py生成.exe的方法和报错总结
Pyinstaller 打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié)
簡(jiǎn)介
有時(shí)候自己寫(xiě)了個(gè)python腳本覺(jué)得挺好用想要分享給小伙伴,但是每次都要幫他們的電腦裝個(gè)python環(huán)境。雖然說(shuō)裝一下也快,但是相對(duì)來(lái)說(shuō)效率還是不高,要是能將python的**.py文件轉(zhuǎn)化為.exe**,那么世界將變得更美好。這篇文章我將簡(jiǎn)單的介紹如何使用Pyinstaller來(lái)打包我們的python腳本。
安裝 Pyinstaller
如果有什么問(wèn)題一般都能夠在這里找到解答。
安裝 pyinstaller還是非常的方便的,只需要一句話(huà):
pip install pyinstaller
就OK了
打包 Python腳本
關(guān)于打包其實(shí)也很簡(jiǎn)單,也只需要一句話(huà):
pyinstaller yourprogram.py
當(dāng)然pyinstaller命令的語(yǔ)法是:
pyinstaller [options] script [script ...] | spec文件
在運(yùn)行 pyinstaller命令后在yourprogram.py同一個(gè)目錄下會(huì)產(chǎn)生一個(gè)文件兩個(gè)文件夾:
yourprogram.spec文件,該文件中寫(xiě)了一些配置,可以打開(kāi)進(jìn)行修改,下一次可以直接打包該文件,不用打包yourprogram.py了。
build文件夾,包含一些日志文件和工作文件
dist文件夾,包含可執(zhí)行文件
1. 控制臺(tái)窗口選項(xiàng)
-c, --console, --nowindowed 打包程序運(yùn)行時(shí),打開(kāi)標(biāo)準(zhǔn)i / o控制臺(tái)窗口(默認(rèn))
-w, --windowed, --noconsole 打包程序運(yùn)行時(shí),不打開(kāi)標(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)用程序的名稱(chēng)(默認(rèn)值:第一個(gè)腳本的名稱(chēng))
以上四個(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過(guò)程中出現(xiàn)的錯(cuò)誤和解決方法
1. AttributeError: ‘str' object has no attribute ‘items'
錯(cuò)誤內(nèi)容截取:
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后程序一閃而退,就不知道問(wèn)題出在哪里。
解決方法:
在cmd行里面運(yùn)行你的.exe文件
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
總結(jié)
以上是生活随笔為你收集整理的python打包工具报错_Pyinstaller打包.py生成.exe的方法和报错总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据结构 树(Tree)的基本知识
- 下一篇: python3__绘图__常用数据分析图