【问题标题】:Including a directory using Pyinstaller使用 Pyinstaller 包含目录
【发布时间】:2012-07-04 13:42:32
【问题描述】:

Pyinstaller 的所有文档都谈到了包含单个文件。 是否可以包含一个目录,或者我应该编写一个函数来通过遍历我的包含目录来创建包含数组?

【问题讨论】:

    标签: python installation include pyinstaller packaging


    【解决方案1】:

    【讨论】:

    • 感谢您的分享,使用the Tree class 以优雅的方式为我解决了这个问题。现在我只需要这一行:a.datas += Tree('./dir_to_include', prefix='dir_to_include')
    • 是的,我认为它的文档记录不够好,但这是开源的,所以欢迎 PR :)
    【解决方案2】:

    在spec文件中a = Analysis()之后粘贴以下内容,递归遍历一个目录,并将其中的所有文件添加到分发中。

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

    【讨论】:

    • 对不起,我真的不明白。我有一个名为~/Scripts 的目录。我的数据存储在~/Scripts/Data。我应该用a.datas += extra_datas('Data') 替换a.datas += extra_datas('data') 吗?
    • 如果您的 pyInstaller 脚本也在 Scripts 中并且您在 Scripts 中使用 python mybuildscript.py 调用它,那么是的,您应该使用 Data 替换,否则使用 . 和 @987654332 @ 导航目录树。
    【解决方案3】:

    是的,您只需将目录添加到 Analysis 对象,它们就会被复制。

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

    【讨论】:

    • 至少对于 pyinstaller 4.2 需要将 datas 字段添加为元组。如果你想添加多个文件夹,它需要类似于:... datas = [('test/dir', 'test/dir'),('test2/dir', 'test2/dir')] ...
    【解决方案4】:

    只使用glob 怎么样?

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

    【讨论】:

      【解决方案5】:

      问题比你想象的要容易

      试试这个: --add-data="path/to/folder/*;."

      希望对你有帮助!!!

      【讨论】:

      • 这将包括 path/to/folder/ 中的所有文件,以及 pyinstaller 输出根目录中的子文件夹中的所有文件。但是,不会保留任何子文件夹结构 - 所有文件都将展平到根目录。如果您想将文件包含在它们相同的目录结构中(但它仍然不会保留子文件夹结构),您可以使用--add-data="path/to/folder/*;path/to/folder/"
      猜你喜欢
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多