Nafees Anwar
Nafees Anwar

Reputation: 41

cx_freeze not working with __init__ == __main__

I have a strange problem. I am using cx_freeze to create executable file from python script. I have setup.py file with following code:

from cx_Freeze import setup, Executable;

setup(name='Notebook',
      version='1',
      description='This is a Notebook.',
      executables=[Executable('menu.py')]);

and I execute this file with following command:

python setup.py build

I have several modules in my project. When I directly run my menu like this:

m = Menu()
m.run();

everything works fine. But when I use this check:

if __name__ == '__main__':
    m = Menu()
    m.run();

.exe file just flashes. Can somebody help? Sorry for English. Thanks in Advance,

Upvotes: 4

Views: 1021

Answers (2)

rmmbear
rmmbear

Reputation: 11

I actually encountered the same problem recently, but I did manage to figure it out! It's because when you run the exe, its __name__ is not __main__, but something like name_of_script__main__. So in your case I'm pretty sure it would be menu__main__.

So, to figure out what your __name__ actually is, you have to print(__name__) somewhere in the body of your code.

Upvotes: 1

GFLEdev
GFLEdev

Reputation: 41

I'm using cx_Freeze 5.0 with Python 2.7, and I have exactly the same problem.

And it seems to be a regression since my script used to work with the previous version of cx_freeze (4.3.4).

To be more specific, I made an exe (not a GUI : base=None), and I put some print everywhere. I wrote a bat to call the exe. All the prints are printed, except those which are in the "main section".

I also did the test with the sample SimpleTkApp.py delivered with cx_Freeze. The problem is the same: when the code is put in a main section, it doesn't work anymore : the exe is launched, then stopped, without doing nothing.

Upvotes: 2

Related Questions