用pyinstaller打包帶有openpyxl模塊的項(xiàng)目時,打包完成后,運(yùn)行程序出現(xiàn)報錯,
ModuleNotFoundError: No module named openpyxl.cell.writer'
其本質(zhì)是,pyinstaller打包的時候,通??梢宰詣訖z測到項(xiàng)目所使用的依賴項(xiàng)并將其包含在打包的可執(zhí)行文件中。但是,有些情況下,PyInstaller可能會無法自動檢測到某些依賴項(xiàng),這時就需要使用–hidden-import選項(xiàng)顯式地將它們包含進(jìn)去。
因此,在使用Pyinstaller打包項(xiàng)目時,使用–hidden-import選項(xiàng),告訴PyInstaller需要包含哪些依賴項(xiàng)的方法。
在運(yùn)行PyInstaller時,你應(yīng)該根據(jù)你的代碼中需要的依賴項(xiàng)使用–hidden-import選項(xiàng),以確保所有依賴項(xiàng)都被正確地包含在生成的可執(zhí)行文件中。
所以,使用一下的打包命令可以解決該問題。
pyinstaller -F main.py -n [程序名稱] --hidden-import=openpyxl
By the way, 看到有些博客說,檢索到pyinstaller的hook-openpyxl.py文件內(nèi)容如下,
# ------------------------------------------------------------------
# Copyright (c) 2020 PyInstaller Development Team.
#
# This file is distributed under the terms of the GNU General Public
# License (version 2.0 or later).
#
# The full license is available in LICENSE.GPL.txt, distributed with
# this software.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ------------------------------------------------------------------
# Hook for the openpyxl module: https://pypi.python.org/pypi/openpyxl
# Tested with openpyxl 2.3.4, Python 2.7, Windows
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('openpyxl')
說pyinstaller僅支持openpyxl 2.3.4,該論斷并不合理,有文件內(nèi)容可知,該版本使用openpyxl 2.3.4、Python 2.7、Windows進(jìn)行測試,而非僅支持,特此糾正。
當(dāng)然,如果你的項(xiàng)目中只包含openpyxl庫,而不涉及其他調(diào)用openpyxl的庫文件(如,pandas),則可以簡單的將目前項(xiàng)目中所使用的openpyxl進(jìn)行降級,以便捷的使用pyinstaller進(jìn)行打包,當(dāng)然你需要警惕庫文件降級帶來的某些函數(shù)功能的變化。文章來源:http://www.zghlxwxcb.cn/news/detail-616751.html
如果你在項(xiàng)目中同時使用了pandas等庫文件,需要依賴于openpyxl時 ,如果簡單的按照上述方法進(jìn)行降級操作,則你會遇到新的錯誤(即pandas版本與openpyxl版本不一致等)。文章來源地址http://www.zghlxwxcb.cn/news/detail-616751.html
到了這里,關(guān)于pyinstaller打包openpyxl和pandas包,報錯ModuleNotFoundError openpyxl的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!