0

As personal exercise I developed python application with name Hades which

  • on regular bases do some changes on Windows 10 desktop
  • compiled into .exe with cx_Freeze
  • finally located on network drive E:\ (it is sensible for further story)
  • successfully starts (from E:...) by shortcut(.lnk) on desktop

Usual usage: after login I start app by shortcut and it does some changes on desktop.

As next step of this project I decided to start app automatically on login. Pick up approach #2 from here(How to start a python file while Windows starts?):

  • to the registry folder HKCU\Software\Microsoft\Windows\CurrentVersion\Run
  • added entry Hades = "E:!Soft!RanD\20220808 - My desktop style\Hades.v.0.3\build\exe.win-amd64-3.10\main.exe"
  • on login app starts with error, see screenshot

What is confusing me, that on error screen I see path to C:\ drive location, where project was located initially - it is standard folder for PyCharm projects. Event when I changed registry entry to following values

  • "c:\Users\kapustin.av\PycharmProjects\Hades.v.0.3\build\exe.win-amd64-3.10\main.exe"
  • "c:\Temp\Hades.v.0.3\build\exe.win-amd64-3.10\main.exe"

then I had the same issue on Windows re-login; of course app and related folders were available on declared paths.

On the bottom of error message dialog it is mention log.txt file. It is file created by me in python code. In code (around log.txt as well) there is no direct path to C:... And also I did tests when file existed on C:... path during login. Howerer the message is still the same.

Would it be related to cx_Freeze specific ? Any idea, please, in which direction look forward for resolving the case ?

Andrey K
  • 23
  • 3
  • Generally: Avoid relative paths and check that the code is executed in the right account with appropriate access rights. – Michael Butscher Apr 03 '23 at 09:48
  • @mbutscher.de about "right account" I'm sure, because of a) as I described I succesfully started the app from desktop shortcut under my windows account and b) now trying to start it automatically on login to the same account. Concern about "relative paths" is not clear for me now: I excluded abolute paths from code, because of 'who knows where app is going to be located during next installation?' ) Would you explain this point in more deatails. – Andrey K Apr 03 '23 at 10:29
  • How do you know that the code tries to open the right `log.txt` file? Have you explicitly set the current working directory? If so, how? – Michael Butscher Apr 03 '23 at 12:30
  • @mbutscher.de, you are right, I've not explicitly set the current working directory. I expect that implicitly current working directory is the path where main.exe(result of cx_Freeze) is located. Until your today concern this expectation has been looked correctly. I will check this concern. Also, I found several topics about "working directory". Would you suggest ref to material with more details why to "**avoid** relative paths", from general Python perspective <- for me it is new 'learning point'. – Andrey K Apr 03 '23 at 13:26
  • Avoiding relative paths just ensures indepence from the current working directory. An absolute path can also be assembled together from some absolute path like e. g. the location of the executable plus a relative part. – Michael Butscher Apr 03 '23 at 19:05
  • Thanks, @mbutscher.de: reading-testing around this idea right now and more than 70% sure that root cause is in it. – Andrey K Apr 04 '23 at 08:30

1 Answers1

1

Minimal fix in my code is located in main.py:

import os, sys

if getattr(sys, 'frozen', False):
    os.chdir(os.path.dirname(sys.executable))

Issue consists of two pieces:

  1. My application working directory was not defined explicitly
  2. Due to cx_Freeze usage it is required to find .exe running path; not applicable to use python inspect module.

Related materials:

Thanks to Michael for inspiring to the right research direction.

Andrey K
  • 23
  • 3