简体   繁体   English

使用 Pyinstaller 包含一个目录

[英]Including a directory using Pyinstaller

All of the documentation for Pyinstaller talks about including individual files. Pyinstaller 的所有文档都谈到了包含单个文件。 Is it possible to include a directory, or should I write a function to create the include array by traversing my include directory?是否可以包含一个目录,或者我应该写一个 function 通过遍历我的包含目录来创建包含数组?

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

The problem is easier than you can imagine问题比你想象的要容易

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

hope it helps !!!希望能帮助到你 !!!

Yes, you can just add directories to the Analysis object and they get copied across.是的,您只需将目录添加到 Analysis 对象,它们就会被复制。

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

What about just using glob ?只使用glob怎么样?

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

a.datas = datas

The accepted answer works fine if your folder doesn't have any subfolders.如果您的文件夹没有任何子文件夹,则接受的答案可以正常工作。 However, if you folder does have any subfolders, all data will be collapsed into the your 'dir_to_include' path, which will mess up your imports.但是,如果您的文件夹确实有任何子文件夹,则所有数据都将折叠到您的“dir_to_include”路径中,这会弄乱您的导入。 The below is an example, where main.py accesses music and pictures from the data folder.下面是一个示例,其中 main.py 访问数据文件夹中的音乐和图片。

.
├── data
│   ├── music
│   │   ├── music1.mp3
│   ├── pics
│   │   ├── pic1.png
│   │   ├── pic2.png
│   │   ├── numbers
│   │   │   ├── 0.png
│   │   │   ├── 1.png
│   │   │   ├── 2.png
├── main.py
├── main.spec

In this case, after we generated our main.spec file using pyinstaller main.py , we can change some arguents inside the main.spec file.在这种情况下,在我们使用pyinstaller main.py生成 main.spec 文件后,我们可以更改 main.spec 文件中的一些参数。

On the top of the main.spec, our we set our added_files :在 main.spec 的顶部,我们设置了added_files

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None

added_files = [
    ('./data/*.*', 'data'),
    ('./data/pics/*.*', 'data/pics'),
    ('./data/music/*.*', 'data/music'),
    ('./data/pics/numbers/*.*', 'data/pics/numbers'),
    ]
...

Then, we set datas=added_files below:然后,我们在下面设置datas=added_files

...
a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=added_files,
...

We have to add the path for each individual subfolder, or else they won't get added by pyinstaller automatically.我们必须为每个单独的子文件夹添加路径,否则 pyinstaller 不会自动添加它们。 If we simply do added_files = '[(./data/*', data')] , then all the pictures and audio will be added to the 'data' directory, which is not what we want.如果我们简单地做added_files = '[(./data/*', data')] ,那么所有的图片和音频都会被添加到 'data' 目录中,这不是我们想要的。 Thus, we use path/*.* , so folders don't get added recursively, and define each subfolder manually.因此,我们使用path/*.* ,因此不会递归添加文件夹,而是手动定义每个子文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM