Service fails to start when external log config file is involved

Question:

I am using PyInstaller to bundle my multi-module Python app into a one-file exe. The entry point for this app is a module extending win32serviceutil.ServiceFramework — so this is meant to run as a Windows service. Problem arises when I try to furnish a user configurable logger ini file with this app. In my main module, I set up the logger thus,

log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.conf')
logging.config.fileConfig(log_file_path)

My PyInstaller command is the following:

pyinstaller -F <main-file>.py -n <exe-name> --hidden-import=win32timezone --add-data "logging.conf;."

Once packaged, I install the produced exe as a service and it successfully registers as a Windows service. However when I attempt to start it, it fails.

The interesting bit is that an empty log file is created in my configured location. So this implies that 1) The application did read my config file, and 2) There’s no permissions issue here. Has anyone tried to set up something like this that could help shed light on what I might be missing?

Asked By: shasan

||

Answers:

When using the one-file option with PyInstaller, we cannot validly use the __file__ variable in our code to identify the bundled app or its location. Instead, PyInstaller sets special system variables at runtime, like sys._MEIPASS and sys.executable; which specify the temporary folder created by the bootloader to run the app, and location of the frozen executable (the bootloader), respectively.

Once I changed my path manipulation to use these variables to locate the log config file, the file is successfully read and the service works.

Answered By: shasan