Including a directory using PyInstaller

Question:

All of the documentation for PyInstaller talks about including individual files.

Is it possible to include a directory, or should I write a function to create the include array by traversing my include directory?

Asked By: Simon Knight

||

Answers:

Just use glob:

from glob import glob

datas = []
datas += glob('/path/to/filedir/*')
datas += glob('/path/to/textdir/*.txt')

# ...

a.datas = datas
Answered By: jdi

Paste the following after a = Analysis() in the spec file to traverse a directory recursively and add all the files in it to the distribution.

##### include mydir in distribution #######
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
            rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
        extra_datas.append((f, f, 'DATA'))

    return extra_datas
###########################################

# append the 'data' dir
a.datas += extra_datas('data')
Answered By: styts

There is also the official supported option using Tree():

Pyinstaller: generate -exe file + folder (in –onefile mode)

The TOC and Tree Classes

Answered By: denfromufa

The problem is easier than you can imagine. Try this:

--add-data="path/to/folder/*;."

Answered By: leminhnguyen

Yes, you can just add directories to the Analysis object and they get copied across.

a = Analysis(['main.py'],
             datas = [('test/dir', 'test/dir')],
             ...)
Answered By: wedesoft