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"]}})
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.
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.
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:
- Open the command prompt and uninstall openpyxl with 'pip uninstall openpyxl'
- Reinstall openpyxl using an older version 'pip install openpyxl==2.3.5'
In openpyxl\packaging\extended.py add it at line 5:
__version__ = str(__version__)