PyInstaller and python-docx module do not work together PyInstaller and python-docx module do not work together tkinter tkinter

PyInstaller and python-docx module do not work together


Pyinstaller doesn't include docx modules(I had the same problem, i will write it for others because i couldnt find proper tutorial how to fix it)

If you have Pyinstaller installed use in cmd:

pyi-makespec your_py_file_name.py

This command creates .spec file but does not go on to build the executable.

Open .spec file in Notepad and edit it by adding:

import sysfrom os import pathsite_packages = next(p for p in sys.path if 'site-packages' in p)

And

datas=[(path.join(site_packages,"docx","templates"), "docx/templates")],

My example .spec file looks like this

# -*- mode: python -*-import sysfrom os import pathsite_packages = next(p for p in sys.path if 'site-packages' in p)block_cipher = Nonea = Analysis(['xml_reader.py'],             pathex=['C:\\Users\\Lenovo\\Desktop\\exe'],             binaries=[],             datas=[(path.join(site_packages,"docx","templates"), "docx/templates")],             hiddenimports=[],             hookspath=[],             runtime_hooks=[],             excludes=[],             win_no_prefer_redirects=False,             win_private_assemblies=False,             cipher=block_cipher)pyz = PYZ(a.pure, a.zipped_data,             cipher=block_cipher)exe = EXE(pyz,          a.scripts,          exclude_binaries=True,          name='xml_reader',          debug=False,          strip=False,          upx=True,          console=True )coll = COLLECT(exe,               a.binaries,               a.zipfiles,               a.datas,               strip=False,               upx=True,               name='xml_reader')

To make exe file type in cmd

pyinstaller your_py_file_name.spec

It should produce dist folder where is your executable


A big problem is that if there is something wrong the error gets lost:

from tkinter import *import tkinter.messageboxroot = Tk()def Word():    wordsaveask = tkinter.messagebox.askquestion("Save", "do you want to save?")    if wordsaveask == ("yes"):        with open('Titan Tech.docx',"w") as f:            f.write("TEST")        tkinter.messagebox.showinfo('Saved', 'The file was made')    else:        tkinter.messagebox.showinfo("ok","did not save")button = Button(root,text="test",command=Word)button.pack()mainloop()

This works for me when running normally but fails when inside a packaged app, and without access to the console I can't see the error messages, so here is a way to display them to the tk application:

from tkinter import *import tkinter.messageboxroot = Tk()def Word():    try: #SOMETHING HERE IS GOING WRONG        wordsaveask = tkinter.messagebox.askquestion("Save", "Are you sure you want to export to a Microsoft Word file?")        if wordsaveask == ("yes"):            with open('Titan Tech.docx',"w") as f:                f.write("TEST")            tkinter.messagebox.showinfo('Save', 'The program has exported the information to a Microsoft Word document!')        else:            tkinter.messagebox.showinfo("ok","did not save")    except:        import traceback        tkinter.messagebox.showinfo("ERROR",traceback.format_exc())        raise # Errors should never pass silently. - The Zen of Python, by Tim Petersbutton = Button(root,text="test",command=Word)button.pack()mainloop()

once I run this I get this handy little popup:mage of traceback, text is directly below

Traceback (most recent call last):  File "/Users/Tadhg/Documents/codes/test.app/Contents/Resources/main.py", line 10, in Word    with open('Titan Tech.docx',"w") as f:PermissionError: [Errno 13] Permission denied: 'Titan Tech.docx'

This will hopefully let you debug your application to figure out what is going on, hope it helps :)