[英][Python][cx-freeze] ImportError: cannot import name 'ExcelFormulaParser'
Hi I encounter an issue when running cx-freeze on a piece of python code.嗨,我在一段 python 代码上运行 cx-freeze 时遇到问题。 It always threw error message when I click the executable file generated by cx-freeze.当我单击 cx-freeze 生成的可执行文件时,它总是抛出错误消息。 Anyone can help?任何人都可以帮忙吗? - Python 3.6.1 used. - 使用 Python 3.6.1。
I also ran the cx-freeze on another piece of python code and it worked well.我还在另一段 python 代码上运行了 cx-freeze,它运行良好。
Error message is as below:错误信息如下:
Last login: Thu Aug 31 14:45:12 on ttys002
EMacBook-Pro:~ E$ /Users/E/PycharmProjects/ImageRename/dist/exportImageName_1 ; exit;
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
module.run()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Freeze/initscripts/Console.py", line 26, in run
exec(code, m.__dict__)
File "exportImageName_1.py", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xlwt/__init__.py", line 4, in <module>
from .Worksheet import Worksheet
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xlwt/Worksheet.py", line 38, in <module>
from .Row import Row
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xlwt/Row.py", line 8, in <module>
from . import ExcelFormula
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xlwt/ExcelFormula.py", line 3, in <module>
from . import ExcelFormulaParser, ExcelFormulaLexer
ImportError: cannot import name 'ExcelFormulaParser'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[进程已完成]
Source Code in the py file: py文件中的源代码:
import xlwt
import os
import FileDirectory_1
def walk_dir(dir):
rowindex = 1
for root, dirs, files in os.walk(dir):
for f in files:
if "jpg" in f:
table.write(rowindex,0, root)
table.write(rowindex,1, f)
# print(os.path.join(root,f))
rowindex += 1
dir = FileDirectory_1.DIR;
excelName = "imageRename.xls"
# =======Excel Style============
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = "Arial"
font.bold = True
style.font = font
# ==============================
file = xlwt.Workbook()
table = file.add_sheet("ImageRename", cell_overwrite_ok=True)
table.write(0, 0, "Old File Path", style)
table.write(0, 1, "Old Image Name", style)
table.write(0, 2, "New Image Name", style)
walk_dir(dir)
file.save(os.path.join(dir, excelName))
print(excelName, " has been generated.")
Found the issue.发现问题。 I checked the files in the /lib/xlwt folder which was generated by cx-freeze and found "ExcelFormulaParser.pyc" and "ExcelFormulaLexer.pyc" files are somehow missing.我检查了由 cx-freeze 生成的 /lib/xlwt 文件夹中的文件,发现“ExcelFormulaParser.pyc”和“ExcelFormulaLexer.pyc”文件不知何故丢失。 After adding back the "ExcelFormulaParser.py" and "ExcelFormulaLexer.py" files the issue was resolved.添加回“ExcelFormulaParser.py”和“ExcelFormulaLexer.py”文件后,问题得到解决。 These two files were copied from my own xlwt files from the site package to the build folder.这两个文件是从我自己的xlwt文件从站点包复制到build文件夹的。
Actually there isn't any need to copy files.实际上没有任何需要复制文件。 Simply add xlwt as a package dependency in the cx-freeze setup.py file.只需在 cx-freeze setup.py 文件中添加 xlwt 作为包依赖项。
eg:例如:
build_exe_options = {"packages": [...your packages....., "xlwt"]
...............
}
I had the same issue and this will fix it.我有同样的问题,这将解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.