pythonwindowspython-3.xexecutablecx-freeze

cx_Freeze with python packages (not just one module)


All of the cx_Freeze examples are for one file (module). I need to make an executable for an entire python package. Why is that hard to make?

Here's my directory:

test1/
  __init__ 
  __main__

The way I run this from the command line is by using the following cmd

python -m test1 

__init__ is empty and __main__ just have a simple print statement. I am using python 3.5.1 but I can switch to python 3.4 if that would fix the problem

Here is my setup.py for win64

from cx_Freeze import setup, Executable
import sys

build_exe_options = {"packages": ['test1'],
                     "include_files": []
                    }
executables = [
                Executable("__main__")
              ]
setup(
    name = "Foo",
    version = "0.1",
    description = "Help please!",
    author = "me",
    options = {"build_exe": build_exe_options},
    executables = executables
)

Update: 1- see the comment below for solution for this approach 2- switch to pyinstaller because it can produce one exe file not a folder


Solution

  • Freezing a whole package doesn' t make sense because to create an executable binary you will want a Python script that can be run standalone from the command line. A package normally isn't started out-of-the-box but would get imported by another module.

    However you can always import a package in your script so when you freeze it the package gets included in your distribution.

    So do something like this:

    test1/
      __init__ 
      __main__
    run_test.py
    

    run_test.py now imports test1 and starts your function that does whatever you want.

    import test1
    run_the_whole_thing()
    

    Note: You will need to change your Executable in the setup.py to run_test.py.