使用pyinstaller将python打包成exe可执行程序出错,查看错误原因(黑框)的方法

文章讲述了如何将包含openpyxl库的Python程序打包成exe文件,遇到的ModuleNotFoundError问题,以及通过添加`--hidden-import`参数来解决导入特定模块的技巧。
摘要由CSDN通过智能技术生成

一开始想把包含openpyxl库的python程序用pyinstaller打包成exe文件,程序需要用户输入:

# -*- coding: utf-8 -*-

from openpyxl import Workbook
wb = Workbook()
 
# grab the active worksheet
ws = wb.active

number=input("please input number:")
# Data can be assigned directly to cells
ws['A1'] = number
 
# Rows can also be appended
ws.append([1, 2, 3])

 
# Save the file
wb.save("sample.xlsx")

使用pyinstaller时

​pyinstaller -F -c excel_test.py

报错了   但报错的那个黑框很短时间就自动关闭了  要想查看为什么错了   可以把pyinstaller参数中的-c 换成-w  也就是在运行程序时不需要那个黑框(注意:换成-w时候就不能用户输入了,需要先把代码里的input的值写死)

​pyinstaller -F -w excel_test.py

就可以运行exe看到错误的原因了

 显示

Traceback (most recent call last):
  File "excel_test.py", line 8, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "openpyxl\__init__.py", line 6, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "openpyxl\workbook\__init__.py", line 4, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "openpyxl\workbook\workbook.py", line 9, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "openpyxl\worksheet\_write_only.py", line 13, in <module>
  File "openpyxl\worksheet\_writer.py", line 23, in init openpyxl.worksheet._writer
ModuleNotFoundError: No module named 'openpyxl.cell._writer'


然后在网上查了  需要使用 pyinstaller -F -c --hidden-import=openpyxl excel_test.py

但还是报相同的错

解决方法:

使用

pyinstaller -F -c --hidden-import=openpyxl.cell._writer excel_test.py

引入具体的openpyxl.cell._writer模块即可解决


同理  如果之后报错显示缺少win32timezone  那么在程序中import引入win32timezone即可运行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值