cx_freeze attributeerror: 'windowspath' object has no attribute 'write' error

Question:

I got this error while trying to convert my .py file to .exe file with Cx_Freeze:

cx_freeze attributeerror: 'windowspath' object has no attribute 'write' error

The file I’m trying to convert:

print("Hello!")
while True:
    pass

Setup.py file:

from cx_Freeze import setup, Executable
 
setup( name = "HW" , 
    version = "0.1" ,
    description = "Hello World App" , 
    executables = [Executable("file.py")] , 
     )

The full console output(only errors):

Traceback (most recent call last):
  File "C:UsersMuhammedDesktopa.py", line 6, in <module>
    executables = [Executable("b.py")] ,
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libsite-packagescx_Freezedist.py", line 447, in setup
    setuptools.setup(**attrs)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilscore.py", line 148, in setup
    dist.run_commands()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilsdist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilsdist.py", line 974, in run_command
    cmd_obj.run()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilscommandbuild.py", line 135, in run
    self.run_command(cmd_name)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilscmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libdistutilsdist.py", line 974, in run_command
    cmd_obj.run()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libsite-packagescx_Freezedist.py", line 317, in run
    freezer.Freeze()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libsite-packagescx_Freezefreezer.py", line 623, in Freeze
    self._write_modules(library_zip, finder)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libsite-packagescx_Freezefreezer.py", line 568, in _write_modules
    outFile.writestr(zinfo, data)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1644, in writestr
    with self.open(zinfo, mode='w') as dest:
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1348, in open
    return self._open_to_write(zinfo, force_zip64=force_zip64)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1461, in _open_to_write
    self.fp.write(zinfo.FileHeader(zip64))
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 721, in write
    n = self.fp.write(data)
AttributeError: 'WindowsPath' object has no attribute 'write'
Exception ignored in: <bound method ZipFile.__del__ of <zipfile.PyZipFile [closed]>>
Traceback (most recent call last):
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1649, in __del__
    self.close()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1667, in close
    self._write_end_record()
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 1769, in _write_end_record
    self.fp.write(endrec)
  File "C:UsersMuhammedAppDataLocalProgramsPythonPython36libzipfile.py", line 721, in write
    n = self.fp.write(data)
AttributeError: 'WindowsPath' object has no attribute 'write'

I was not getting this error when using Windows 10, but I started getting this error when I upgraded my system to Windows 11.

Asked By: MAMOSKO

||

Answers:

I solved my problem like this:
My Python version was 3.6. I got errors in many modules because of the python version. Thinking maybe that’s why, I installed 3.8 and tried again. This way my problem was solved.

I also tried it on Windows 7 with the help of Virtual Box, I thought the problem was caused by Windows 11. I got the same error, upgraded to 3.8 and the problem was solved again.

(I used Google Translate)

Answered By: MAMOSKO
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.