PyInstaller 未捆绑特定文件夹及其内容

革命

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

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”。当我将模型文件夹与二进制文件放在同一文件夹中时,项目按预期工作,但我无法将其与其他文件、文件夹和二进制文件捆绑到同一个“onefile”中。我错过了什么。

革命

我想通了我做错了什么。我会在这里分享,以防其他人像我一样陷入困境。在我加载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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章