python打包工具报错_Pyinstaller打包.py生成.exe的方法和报错总结
Pyinstaller 打包.py生成.exe的方法和報錯總結
簡介
有時候自己寫了個python腳本覺得挺好用想要分享給小伙伴,但是每次都要幫他們的電腦裝個python環境。雖然說裝一下也快,但是相對來說效率還是不高,要是能將python的**.py文件轉化為.exe**,那么世界將變得更美好。這篇文章我將簡單的介紹如何使用Pyinstaller來打包我們的python腳本。
安裝 Pyinstaller
如果有什么問題一般都能夠在這里找到解答。
安裝 pyinstaller還是非常的方便的,只需要一句話:
pip install pyinstaller
就OK了
打包 Python腳本
關于打包其實也很簡單,也只需要一句話:
pyinstaller yourprogram.py
當然pyinstaller命令的語法是:
pyinstaller [options] script [script ...] | spec文件
在運行 pyinstaller命令后在yourprogram.py同一個目錄下會產生一個文件兩個文件夾:
yourprogram.spec文件,該文件中寫了一些配置,可以打開進行修改,下一次可以直接打包該文件,不用打包yourprogram.py了。
build文件夾,包含一些日志文件和工作文件
dist文件夾,包含可執行文件
1. 控制臺窗口選項
-c, --console, --nowindowed 打包程序運行時,打開標準i / o控制臺窗口(默認)
-w, --windowed, --noconsole 打包程序運行時,不打開標準i / o控制臺窗口(默認)
2. 軟件圖標選項
-i FILE.ico , --icon FILE.ico 為你的程序添加一個圖標
3. 版本信息選項
--version-file FILE.txt 可以將您的版本信息添加到你的軟件當中
這個FILE.txt的文件內容為:
# 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. 生成結果選項
-D,--onedir 這會創建一個包含可執行文件的單文件夾包(默認)
-F,--onefile 這個只會創建一個可執行文件
--specpath DIR 用于存儲生成的spec文件的文件夾(默認值:當前目錄)
-n NAME, --name NAME 應用程序的名稱(默認值:第一個腳本的名稱)
以上四個就是常用的參數,可以根據自己的需求進行設置。
例子
下面這個是我打包一個“自動更新桌面的腳本”的命令:
G:\PyinstallerTest\Auto_Wallpaper>pyinstaller -F -i G:\PyinstallerTest\Auto_Wallpaper\icon\moon.ico --version-file=version.txt Auto_Wallpaper_of_Moon.py
執行后就能夠成功的將**.py腳本轉化為.exe**可執行文件。
錯誤總結
在這里我會總結我在使用pyinstaller過程中出現的錯誤和解決方法
1. AttributeError: ‘str' object has no attribute ‘items'
錯誤內容截取:
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. 打包后運行.exe程序調試閃退
有時候將.py文件打包成.exe文件后,不一定能夠正常運行。但是點.exe后程序一閃而退,就不知道問題出在哪里。
解決方法:
在cmd行里面運行你的.exe文件
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
總結
以上是生活随笔為你收集整理的python打包工具报错_Pyinstaller打包.py生成.exe的方法和报错总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构 树(Tree)的基本知识
- 下一篇: python3__绘图__常用数据分析图