Search code examples
pythoncx-freeze

How to detect if program is being compiled by cx_freeze?


I have a python program that reads an ini using configparser. I want to have the same name as the python file that's why I use following code:

# nativ python
#fileName = sys.argv[0].split('.')[0]

# compiled python
fileName = sys.argv[0].split('\\')[-1]
fileName = fileName.split('.')[0]

As you can see I have different versions depending on the mode I run it. Running it as python script sys.argv gives a different result than when using it as an exe. I toggle this by hand and sometimes I forget it, having to change it and having to recompile the whole thing. But I saw other things like:

if sys.platform == "win64":
    base = "Win64GUI"
else:
    base = "Win32GUI"

I was wondering if I could detect if the source is being compiled by cx_freeze using something similar to this:

if getting_compiled():
    fileName = sys.argv[0].split('.')[0]
else:
    fileName = sys.argv[0].split('\\')[-1]
    fileName = fileName.split('.')[0]

Solution

  • Yes, you can check the value of "sys.frozen", which is set by cx_Freeze.

    getattr(sys, "frozen", False)

    should do it for you.