Py2Exe openpyxl importerror

旧城冷巷雨未停 提交于 2020-01-03 19:08:36

问题


I have a python application that depends on openpyxl and works well when running it through the python interpreter. However, when creating an exe with py2exe. The exe was generated but when I click on it I get an error and the following log is generated:

Traceback (most recent call last):
File "excelTest.py", line 1, in <module>
File "openpyxl\__init__.pyc", line 30, in <module>
File "openpyxl\workbook\__init__.pyc", line 5, in <module>
File "openpyxl\workbook\workbook.pyc", line 16, in <module>
File "openpyxl\writer\write_only.pyc", line 23, in <module>
File "openpyxl\writer\excel.pyc", line 36, in <module>
File "openpyxl\packaging\extended.pyc", line 4, in <module>
ImportError: cannot import name __version__

Could anyone let me know what the problem is and how I can fix it. Here is my setup.py:

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(windows=['excelTest.py'], options={"py2exe": {"includes": ["openpyxl","os","ntpath","Tkinter","tkFileDialog","sys"]}})

回答1:


I had the same problem.

First, I tried the proposed solution of downgrading to 2.3, but I use read-only functions that didn't work.

Then, reading some openpyxl forums I found that the problem is that 2.4 uses a Jason file for the configuration. But I couldn't instruct py2exe to include it and use it.

Finally, I used pyInstaller, and it worked at the first try.




回答2:


The issue is because __version__ is read from .constants.json file and it is not being taken up by py2exe. For work around, I edited the library file openpyxl\packaging\extended.py

#from openpyxl import __version__
__version__ = "2.4.5"

I commented the import and created a variable __version__ with the version text from .constants.json file present in openpyxl library. Again created the executable using py2exe.

Worked fine for me.




回答3:


I was having the same problem using openpyxl 2.4.3 . I found that to create a .exe file you have to revert to an older version of openpyxl. To do so:

  1. Open the command prompt and uninstall openpyxl with 'pip uninstall openpyxl'
  2. Reinstall openpyxl using an older version 'pip install openpyxl==2.3.5'



回答4:


In openpyxl\packaging\extended.py add it at line 5:

__version__ = str(__version__)


来源:https://stackoverflow.com/questions/42411621/py2exe-openpyxl-importerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!