2

I've built a standalone desktop app using Dash by Plotly. To deploy it I'm using pyinstaller to create an executable and Inno Setup to create an Installation Wizard for the user to install the app. When I use pyinstaller to create an executable I'm not using the one file option as I've got several configuration files.

When run using the pyinstaller executable the app works as expected. But when I use the installed one it doesn't work. My hunch says it's a write permissions issue per this issue; my app creates some temporary files for intermediary steps. How should I handle this? Is there a temp folder on Windows I can save files to that the app doesn't need special write access to? Should I give write access to my program by default?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
LukeDev
  • 509
  • 4
  • 19
  • 1
    No, never, for no reason whatever. Windows itself will prevent you anyway. Documents go in the Documents folder, temp files in the TEMP folder, application data goes into an application-specific folder under ProgramData – Panagiotis Kanavos May 10 '22 at 14:46
  • 1
    The reason for these restrictions is to prevent badly written or malicious programs from deleting other programs' files and data. In fact, when Windows Vista added UAC, it also added [file virtualization](https://learn.microsoft.com/en-us/previous-versions/technet-magazine/cc138019(v=msdn.10)?redirectedfrom=MSDN) so attempts to write to `Program Files` are redirected to a per-user folder – Panagiotis Kanavos May 10 '22 at 14:53
  • 1
    Almost two decades ago, there were tons of articles/materials guiding developers to migrate their applications to Windows Vista/Windows 7 and talking about how to design well with UAC in mind. Your question is equivalent, so you should get started from articles like https://learn.microsoft.com/en-us/previous-versions/aa905330(v=msdn.10) or previous discussions on Stack Overflow. – Lex Li May 10 '22 at 16:16
  • Related question: [Application does not work when installed with Inno Setup](https://stackoverflow.com/q/44333839/850848). – Martin Prikryl May 13 '22 at 08:11

1 Answers1

2

The answer, as stated by @PanagiotisKanavos in the comments, isn't to give write access to Program Files. The correct way to implement this is using the tempfiles module of python.

import tempfile

def functionA():
    temp_file = tempfile.TemporaryFile()
    temp_file.write("hello world")
    contents = temp_file.read()
    temp_file.close()

def functionB():
    temp_file = tempfile.NamedTemporaryFile()
    file_path = temp_file.name
    temp_file.close()

Note that tempfile deletes a temporary file when it is closed. If you intend to use the file on a windows platform it must be closed before it can be read by another process. To do this use the delete=False flag when instantiating the TemporaryFile object. Note that you will have to delete the file manualy. This is easily done by using a named temporary file and deleting the file at that path once you are done with it.

import tempfile

def functionC():
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    temp_file.write("hello world")
    temp_path = temp_file.name
    temp_file.close()
    os.system(f"echo {temp_path}")
    os.remove(temp_path)
LukeDev
  • 509
  • 4
  • 19
  • Probably this will create a temp file in the %TEMP% folder? Assuming that's the case you can just open %TEMP% directly in Windows Explorer if that's helpful when debugging or testing. – StayOnTarget May 11 '22 at 20:08