1

I am working on a script to do some local file management tasks on computer startup. As part of its regular operation, it will occasionally create some temp files in the same folder as it is running from.

In order to keep this as simple as possible, I compiled the script with Pyinstaller with the --onefile and --noconsole arguments, and added the path to the compiled .exe file to the registry, under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. The script works properly while in a development environment, or if started up manually. However, if it starts by means of starting up the computer (through the reg key), it fails to create the files it is supposed to in the right place. After some tests, I have concluded that the file path changes depending on how the file is run:

If started up manually (e.g., double clicking the .exe file or running the .py script):

    print(os.path.dirname(os.path.realpath(__file__)))

Correctly yields the file folder:

    C:\Projects\Test

But if the script starts through run at startup, the same line yields a different location:

    C:\Windows\system32

This causes file operations to be conducted in the system32 folder, which is not supposed to happen.

Is there any explanation for this behavior? Batch files executed by the script to perform file operations (for example, restarting the script itself) also fail if they use relative instead of absolute paths, for the same reasons stated above. How to fix this other than hardcoding the absolute file paths everywhere?

JSantos
  • 13
  • 3
  • When you say the scripts run through start up, how have you achieved it? By copying the script file into Start up folder? – DineshKumar Jun 06 '16 at 15:01
  • No, I added a registry key to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run specifying the file location. I believe this is functionally the same thing though. – JSantos Jun 06 '16 at 15:04
  • I specifically asked this question because os.path.dirname(os.path.realpath(__file__)) will return the current working directory wherever "__file__" is located. For ex , if the file is located in D:/Python it will return the same. For some reason, if it is getting executed from C:/windows/system32 then there are chances tht u might be run into this problem. – DineshKumar Jun 06 '16 at 15:16
  • 1
    Shouldn't a frozen script use `sys.argv[0]` or `sys.executable` instead of `__file__` to get the application directory? The output looks to me like you're printing the working directory, `os.getcwd()` -- not the application directory. It doesn't make sense for the executable to be in the System32 directory. But when executed via the `Run` key, System32 should be the working directory. BTW, don't assume you have write access to the application directory. Temporary files should be created in `os.environ['TEMP']`. – Eryk Sun Jun 06 '16 at 15:29
  • 6 years later, but this is it: https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller – cubicarray Aug 25 '22 at 02:53
  • Specifically, this answer made it for me: https://stackoverflow.com/a/42615559/13055841 – cubicarray Aug 25 '22 at 02:54

0 Answers0