Search code examples
pythoncx-freeze

Adding files and folders using cx_Freeze


I definitively switched from py2exe to cx_Freeze, at the moment I can say it works great for me, I only have some problems with its documentation that is not totally clear to me and it is controversial because around the web there are working solutions totally different from those I found on the official documentation. Especially I did not find a solution to copy both single files located in different source folders and complete folders. For instance I would like that cx_Freeze copies everything from src/locales to src/build/exe.win-amd64-3.8/locales and src/key.ico to src/build/exe.win-amd64-3.8/key.ico


Solution

  • The include_files option of the build_exe command should provide the solution you are looking for. According to the cx_Freeze documentation:

    list containing files to be copied to the target directory; it is expected that this list will contain strings or 2-tuples for the source and destination; the source can be a file or a directory (in which case the tree is copied except for .svn and CVS directories); the target must not be an absolute path

    Accordingly, try the following in your setup.py file:

    from cx_Freeze import setup, Executable
    
    build_exe_options = {"include_files": ["locales", "key.ico"],
                         ...
                        }
    
    setup(
        ...
        options = {"build_exe": build_exe_options},
        ...
    )
    

    With this, cx_Freeze should make the expected copies, provided your setup.py file is located in src and you run the command

    python setup.py build
    

    from there as well.