Home > Net >  Logging module leads to crash after creating .exe
Logging module leads to crash after creating .exe

Time:08-19

I am creating a larger python (v3.9) program which needs to run on a different, single core machine without any IDE or Python installed (both Win 10). For that use, I create an .exe file with PyInstaller:

pyinstaller main.py --name myProgram --clean --noconfirm --add-data="data;data" -i "data/Image.ico"

The code works on my main machine, but on the target machine the program crashes. After some debugging I found the crash to be at a logging step. I use logging to create a simple log, which works when the program is startet from the IDE or the .exe on my main machine, but fails at some point when the .exe is run on the target machine:

import logging

<code>

logging.basicConfig(filename='myProgram.log', format='%(asctime)s - %(levelname)s: %(message)s', datefmt='%d.%m.%y %H:%M:%S', level=logging.INFO)
logging.info("Program started.")

<more code>

print("A")                       # "A" is printed on both machines command lines
logging.info("some message")
print("B")                       # "B" is printed only on main machine, crash on target machine

What confuses me additionally is that I can't see any Error or Exception in the command line, even with something like this the program just stops:

try:
   logging.info("some message")
except Exception as e:
   print(e)

So far, I am at a loss at narrowing the error down further. What could lead to the correct logging of the first message and a crash on a logging attempt with the second message? I am thankful for any ideas for a solution or even for finding a better unterstanding of the problem itself.

CodePudding user response:

Try computing an absolute path for the log filename and make sure the directory where the file should be exists and is writable by the created program.

CodePudding user response:

I found the solution. After putting a couple more logging statements in my code, I could narrow it down to another line between both logging statemens in the original post:

locale.setlocale(locale.LC_ALL, "de_DE")

After commenting that line, the program runs without crashes. I don't know why this happens, but this question had the same problem.

  • Related