0

I have a simple script:

def get_log_dir():
    time_stmp = datetime.now().strftime('%m%d-%H%M%S')
    dir_ = Path('..') / f'log_{time_stmp}'
    dir_.mkdir(exist_ok=True)
    return dir_


if __name__ == '__main__':
    path = get_log_dir()

I build it to exe with pyinstaller --debug all -n minimal minimal.py. Then I create a simple installer with NSIS:

  Name "myAppName"
  OutFile ".\myApp.exe"

  SilentInstall silent

  InstallDir "$LOCALAPPDATA\MyApp-0.1.1"
  
  RequestExecutionLevel admin

Section "Main Section" SecMain

  SetOutPath "$INSTDIR"
  
  File /r ".\myApp-app\*"

  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "myApp" '"$InstDir\minimal.exe"'

  ;Store installation folder
  WriteRegStr HKCU "Software\myApp" "" $INSTDIR
  
  Exec '"$INSTDIR\minimal.exe"'

SectionEnd

After running the installer i get my exe put to AppData folder and there is a registry entry in HKLM. It correctly starts at the end of installation, I can also start it manually at any time. However, if I restart my machine the script starts as it is supposed to but it fails with Access Denied error while trying to create folder. What could be causing this and/or how to debug this? Are there any Windows logs that could be investigated? Maybe pyInstaller logs?

Update: Interestingly it works if instead of modifying registry I create a shortcut and put it to C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. The question now is how does it differ and why? Is the walk around a clean solution or should be considered a hack?

dylan_fan
  • 680
  • 1
  • 5
  • 18

1 Answers1

0

The difference between a Run entry and a shortcut is that a shortcut can provide additional properties like setting the current directory.

This leads me to believe that Path('..') in your Python script is the problem because the current directory is unspecified for a Run entry and in real life is probably the Windows directory where you don't have write access.

The solution is to replace .. with the full path of the .exe. Trying to find the path of the current script is surprisingly tricky. The appdirs package can also help you find an appropriate directory for log files...

Anders
  • 97,548
  • 12
  • 110
  • 164