I am trying to make an executable of my program to give to my FTC team. Everything works but when I try to use my script that includes python-docx in it but it does not comp
Ok so first lets just work with the Minimal Verifiable Example shall we?
from tkinter import *
import tkinter.messagebox
root = 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 fine in the terminal but mysteriously doesn't get to the successfully saved message when opening after packing into an application.
You said that you don't get any error messages but without a terminal or ide raise TypeError
would be invisible to you, so lets put something in place to redirect the message:
from tkinter import *
import tkinter.messagebox
root = 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 Peters
button = Button(root,text="test",command=Word)
button.pack()
mainloop()
once I run this I get this handy little popup:
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'
Now yes I am using a mac and no I am not using PyInstaller
(made my own app builder with pure python code) but I still think this is a valid answer because it will tell you how to proceed with debugging. If you post the error message you get I'd be happy to help out more.
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 sys
from os import path
site_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 sys
from os import path
site_packages = next(p for p in sys.path if 'site-packages' in p)
block_cipher = None
a = 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