Including a directory using Pyinstaller

此生再无相见时 提交于 2019-12-17 15:39:50

问题


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?


回答1:


I'm suprised that no one mentioned the official supported option using Tree():

https://stackoverflow.com/a/20677118/2230844

https://pythonhosted.org/PyInstaller/advanced-topics.html#the-toc-and-tree-classes




回答2:


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')



回答3:


What about just using glob?

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

a.datas = datas



回答4:


The problem is easier than you imagine

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

hope it helps !!!



来源:https://stackoverflow.com/questions/11322538/including-a-directory-using-pyinstaller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!