【问题标题】:PyInstaller not bundling a specific folder and its contentsPyInstaller 未捆绑特定文件夹及其内容
【发布时间】:2019-11-27 01:07:52
【问题描述】:

我一直在尝试使用 PyInstaller 将一个项目捆绑到一个文件中。我已经成功地添加了所有必需的二进制文件和其他文件,除了一个文件夹中的文件,我尝试了其他类似问题的解决方案,如 thisthis 无济于事。我也经历了documentation,但我认为我仍然缺少一些东西。我尝试使用相对路径和绝对路径进行添加。 我的项目结构如下,

Project_Root_Folder
    model(folder)
        model.json file
        .h5 file
    other_data_folders
    other.py files
    other_binaries

我的规范文件,

import PyInstaller.utils.hooks as hooks
from glob import glob
import os
from PyInstaller.utils.hooks import collect_submodules, collect_data_files
tensorflow_location = '/home/user/miniconda3/envs/project/lib/python3.7/site-packages/tensorflow'
tensorflow_binaries = []
for dir_name, sub_dir_list, fileList in os.walk(tensorflow_location):
  for file in fileList:
    if file.endswith(".so"):
      full_file = dir_name + '/' + file
      print(full_file)
      tensorflow_binaries.append((full_file, '.'))

def resource_path(relative):
    return os.path.join(os.environ.get("_MEIPASS2", os.path.abspath(".")), relative)

block_cipher = None
added_binaries = [('_pytransform.so','.'),('lanms/adaptor.so','.')]
#added_files = collect_data_files('nltk',False)
added_files = [
        ('pytransform.*','.'),
        #('/home/user/nltk_data',"nltk_data"),
        ('lanms/*','lanms'),
        (resource_path('model/*'),'model'),
        (resource_path('model/model.json'),'model') 

hidden_imports = []+collect_submodules('scipy.ndimage')+collect_submodules('shapely.geometry')
added_binaries = added_binaries + tensorflow_binaries
__file__ = 'run.spec'

cur_dir = os.path.dirname(os.path.abspath(__file__))
a = Analysis(['run.py'],
             pathex=[cur_dir,
              ],
             binaries=[('./_pytransform.so','.')]+tensorflow_binaries,
             datas=added_files,
             hiddenimports=hidden_imports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='project',
          debug=False,
          strip=False,
          upx=True,
          console=True )

捆绑过程完成并运行二进制文件后,它说找不到“model/model.json”。当我将模型文件夹与二进制文件放在同一个文件夹中时,项目按预期工作,但我无法将它与其他文件、文件夹和二进制文件捆绑到同一个“一个文件”中。我错过了什么。

【问题讨论】:

    标签: python pyinstaller


    【解决方案1】:

    我发现我做错了什么。我会在这里分享它,以防其他人陷入与我一样愚蠢的问题。在我加载json文件的程序中,我应该使用resource_path函数加载它。我之前在spec文件中使用了resource_path函数。

    def resource_path(relative):
        return os.path.join(
            os.environ.get(
                "_MEIPASS2",
                os.path.abspath(".")
            ),
            relative
        )
    json_file = open(resource_path(previously_used_path),'r')
    

    【讨论】:

    • 确保路径根据应用程序是在其开发环境还是部署环境中动态变化。
    猜你喜欢
    • 2012-09-19
    • 2014-04-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2011-11-16
    • 2017-07-15
    相关资源
    最近更新 更多