Search code examples
pythoncx-freeze

Python script not writing on files after cx_Freeze


I am working on a script I intend to freeze with cx_Freeze. I'm using Python 3.6 and cx_Freeze 5.1.1.

The problem I am facing at the moment is that my Python script -- perfectly working as .py --, once frozen with cx_Freeze, does read the content of a text.txt file but seems unable to write on it.

I have written a simplified version of what I'm trying to do and the problem is still there.

This is my main.py:

from tkinter import *

def writing():
    word = str(word_field.get())
    ft = open('text.txt', 'w')
    ft.write(word)
    ft.close()

def from_file():
    ft = open('text.txt', 'r')
    string = ''
    for line in ft:
        line = line.strip()
        string = string+line
    ft.close()

    root2 = Tk()
    result = Label(root2, text=string)
    result.grid(row=1, column=1)
    root2.mainloop()

 root = Tk()
 root.title('My window')
 word_field = Entry(root)
 btn_1 = Button(root, text='Read', command=from_file)
 btn_2 = Button(root, text='Write', command=writing)

 word_field.grid(row=1, column=1, columnspan=2)
 btn_1.grid(row=2, column=1)
 btn_2.grid(row=2, column=2)

 root.mainloop()

And this is the setup.py that I used for cx_Freeze

from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

setup(
    name = "Prova",
    version = "1.0.0",
    options = {"build_exe": {
            'packages': ["tkinter"],
            'include_files' : [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), \
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), 'text.txt'],
            'include_msvcr': True,
            }},
        executables = [Executable("main.py", base="Win32GUI")]
        )

Any idea about why it does behave like this? Thank you in advance!!


Solution

  • An update on this question: After a good number of different configurations (and I even tried to use PyInstaller instead of cx_Freeze), it comes out that the problem wasn't in the script or in the freezing process itself but in the fact that, as the executable file requires to write on a file this is in conflict with the privileges given to the executable.

    This means that the executable cannot write on the file, the program stops but no error message is generated (not even running it in the cmd window). I will create a new dedicated question and I will then post here the link to it.