一.pyinstaller中打包的那些坑
1.python3.5以上的到github上拉取最新的代碼
2.重點坑:
涉及到cefpython3這個庫的,這個庫因為是c++寫的所有在打包的時候是無法對這個庫進行打包的汉匙,所以解決問題的辦法在于:
(1)告訴pyinstaller拉取
(2)手動復制
(3)書寫腳本
請看重點垃僚,其他的簡單腳本打包都是沒有問題的官边,百度一哈都能解決偎谁,就是關(guān)于這個cefpython3包的打包一定要注意問題
- 有控制臺輸出的腳本 不要加 -w 加了就看不到了
- -w 如果是圖像化的界面進行打包棋返,請加上
- pyinstaller -D XX.py 可以進行Debug 看具體的錯誤出現(xiàn)在哪里
- 打包的時候報錯缺少很多的DLL文件這個不影響最后的結(jié)果卷雕,可以不用管他 解決這個問題的方法也是很簡單的钱雷,直接百度就行了
- 打包的時候可以指定 打包的圖標的 和打包的路徑骂铁,這些都可以百度
最后附上專門解決針對cefpython3打包的代碼:
import cefpython3
import subprocess
import os
import shutil
class PyinstallerCefpython:
def __init__(self):
self.no_suffix_script_name = "hello_world"
# cefpython3的包目錄
self.cef_dir = os.path.dirname(cefpython3.__file__)
# 獲取cefpython3包下examples目錄下的hello_world.py
self.script_file = os.path.join(os.path.join(self.cef_dir, "examples"), "hello_world.py")
def delete_before_generates(self):
"""刪除之前打包生成的文件"""
print("*******正在刪除之前打包的生成文件....")
try:
shutil.rmtree("./dist")
shutil.rmtree("./build")
os.remove("{}.spec".format(self.no_suffix_script_name))
except Exception as e:
pass
print("*******刪除成功!")
def script_to_exe(self):
# 相當于執(zhí)行打包命令: Pyinstaller hello_world.py
print("*******開始打包cefpython3應用:", self.script_file)
subprocess.run("Pyinstaller --hidden-import json {}".format(self.script_file))
def copytree(self, src, dst, ignores_suffix_list=None):
print("********正在復制將{}目錄下的文件復制到{}文件夾下....".format(src, dst))
os.makedirs(dst, exist_ok=True)
names = [os.path.join(src, name) for name in os.listdir(src)]
for name in names:
exclude = False
for suffix in ignores_suffix_list:
if name.endswith(suffix):
exclude = True
continue
if not exclude:
if os.path.isdir(name):
new_dst = os.path.join(dst, os.path.basename(name))
shutil.copytree(name, new_dst, ignore=shutil.ignore_patterns(*ignores_suffix_list))
else:
shutil.copy(name, dst)
def solve_dependence(self):
print("*******解決依賴:復制依賴文件到執(zhí)行文件的目錄下....")
self.copytree(self.cef_dir, "./dist/{}".format(self.no_suffix_script_name), [".txt", ".py", ".log", "examples", ".pyd", "__"])
def exec_application(self):
print("*******執(zhí)行成功打包的應用....")
subprocess.run("./dist/{0}/{0}.exe".format(self.no_suffix_script_name))
def run(self):
self.delete_before_generates()
self.script_to_exe()
self.solve_dependence()
self.exec_application()
if __name__ == "__main__":
PyinstallerCefpython().run()
** 附上代碼的原博主:再次感謝http://www.reibang.com/p/1ca206b28e28