61

I'm using Python 2.6 and cx_Freeze 4.1.2 on a Windows system. I've created the setup.py to build my executable and everything works fine.

When cx_Freeze runs, it moves everything to the build directory. I have some other files that I would like included in my build directory. How can I do this? Here's my structure:

src\
    setup.py
    janitor.py
    README.txt
    CHNAGELOG.txt
    helpers\
        uncompress\
            unRAR.exe
            unzip.exe

Here's my snippet:

setup

( name='Janitor',
  version='1.0',
  description='Janitor',
  author='John Doe',
  author_email='john.doe@gmail.com',
  url='http://www.this-page-intentionally-left-blank.org/',
  data_files = 
      [ ('helpers\uncompress', ['helpers\uncompress\unzip.exe']),
        ('helpers\uncompress', ['helpers\uncompress\unRAR.exe']),
        ('', ['README.txt'])
      ],
  executables =
      [
      Executable\
          (
          'janitor.py', #initScript
          )
      ]
)

I can't seem to get this to work. Do I need a MANIFEST.in file?

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

4 Answers4

117

Figured it out.

from cx_Freeze import setup,Executable

includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
includes = []
excludes = ['Tkinter']
packages = ['do','khh']

setup(
    name = 'myapp',
    version = '0.1',
    description = 'A general enhancement utility',
    author = 'lenin',
    author_email = 'le...@null.com',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable('janitor.py')]
)

Note:

  • include_files must contain "only" relative paths to the setup.py script else the build will fail.
  • include_files can be a list of string i.e a bunch of files with their relative paths
    or
  • include_files can be a list of tuples in which the first half of the tuple is the file name with the absolute path and the second half is the destination filename with the absolute path.

(When the lack of the documentation arises, consult Kermit the Frog)

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • Thanks Craig. I tried joining the mailing list that you're on but i wasn't allowed. Could you help me out please? Is it possible to specify the destination directory? the `include_files` option simply created the same directory structure in the builds directory and dumps the included files there. Thanks. – Mridang Agarwalla Jul 13 '10 at 15:42
  • 3
    Solved this too. I went through the source. It's a pity that functions like there aren't documented well enough. I'd love to help to write some documentation for this. – Mridang Agarwalla Jul 13 '10 at 16:12
  • 1
    The `include_files` list can contain both strings with relative path AND tuples with absolute path simultaneously. I had one odd file with an absolute path that I needed, and all the rest of my files were in the project folder. No problem. – Kyle Bradshaw Aug 20 '16 at 18:26
  • 1
    Where is `includes` used for? – Melroy van den Berg Aug 16 '17 at 16:43
  • @danger89 `includes` is used to include **modules**. Very useful as sometimes cx_freeze doesn't include all of the required modules. – S3DEV Aug 22 '19 at 10:09
  • How to include the path of folder and not the file it self? – Scrappy Coco Jun 20 '21 at 01:50
6

There's a more complex example at: cx_freeze - wxPyWiki

The lacking documentation of all the options is at: cx_Freeze (Internet Archive)

With cx_Freeze, I still get a build output of 11 files in a single folder, though, unlike with Py2Exe.

Alternatives: Packaging | The Mouse Vs. Python

Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
  • cx_freeze will never support a single-file .exe file as the dev feels that the 'hacks' used to do such a thing are not clean. If you want a single file then you will have to stick with Py2exe, here is the [feature request for Python3 support](http://sourceforge.net/p/py2exe/feature-requests/20/). – dotancohen Nov 17 '13 at 07:08
  • your link to missing documentation is now dead. If you still use this site, I'd love fore a pointer to the docs on options since I'm coming up short on them – Palu Macil Dec 19 '16 at 20:19
  • J.F. Sebastian fixed the doc link to an archived copy. [The current page](http://cx-freeze.readthedocs.io/en/latest/distutils.html) appears to have less info. – Cees Timmerman Dec 20 '16 at 15:16
3

In order to find your attached files (include_files = [-> your attached files <-]) you should insert the following function in your setup.py code:

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

See cx-freeze: using data files

skrx
  • 19,980
  • 5
  • 34
  • 48
Tiffy
  • 31
  • 1
2

Also you can create separate script that will copy files after the build. It's what I use to rebuild the app on windows (you should have "GNU utilities for win32" installed to make "cp" works).

build.bat:

cd .
del build\*.* /Q
python setup.py build
cp -r icons build/exe.win32-2.7/
cp -r interfaces build/exe.win32-2.7/
cp -r licenses build/exe.win32-2.7/
cp -r locale build/exe.win32-2.7/
pause
greene
  • 637
  • 6
  • 6