【问题标题】:Pyinstaller app: AccessDenied on folder creation running on system startupPyinstaller 应用程序:在系统启动时运行的文件夹创建时访问被拒绝
【发布时间】:2021-06-17 09:51:49
【问题描述】:

我有一个简单的脚本:

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()

我使用pyinstaller --debug all -n minimal minimal.py 将其构建为exe。 然后我用 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

运行安装程序后,我将我的 exe 放入 AppData 文件夹,并且 HKLM 中有一个注册表项。它在安装结束时正确启动,我也可以随时手动启动它。 但是,如果我重新启动我的机器,脚本会按预期启动,但在尝试创建文件夹时它会因访问被拒绝错误而失败。什么可能导致这个和/或如何调试这个?是否有任何可以调查的 Windows 日志?也许是 pyInstaller 日志?

更新: 有趣的是,如果不是修改注册表,而是创建一个快捷方式并将其放入C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup,它就会起作用。现在的问题是它有什么不同,为什么?绕行是一个干净的解决方案还是应该被视为一种黑客行为?

【问题讨论】:

  • minimal.exe 是否请求 UAC 提升?
  • 不,它没有。
  • 当您只为当前用户安装时,您应该使用 RequestExecutionLevel user 并且只使用 HKCU,而不是 HKLM。

标签: python pyinstaller nsis access-denied autostart


【解决方案1】:

运行条目和快捷方式的区别在于,快捷方式可以提供额外的属性,例如设置当前目录。

这让我相信您的 Python 脚本中的 Path('..') 是问题所在,因为当前目录未指定 Run 条目,而在现实生活中可能是您​​没有写入权限的 Windows 目录。

解决方案是将.. 替换为the full path of the .exe。试图找到当前脚本的路径是surprisinglytrickyappdirs package 还可以帮助您找到合适的日志文件目录...

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多