Python PyInstaller未绑定特定文件夹及其内容
我一直在尝试使用PyInstaller将一个项目捆绑到一个文件中。我已经成功地添加了所有必需的二进制文件和其他文件,但一个文件夹中的文件除外,我尝试了其他类似问题的解决方案,如,但没有效果。我也经历了这场比赛,但我想我仍然错过了一些东西。我尝试了使用相对路径和绝对路径进行添加。 我的项目结构如下:Python PyInstaller未绑定特定文件夹及其内容,python,pyinstaller,Python,Pyinstaller,我一直在尝试使用PyInstaller将一个项目捆绑到一个文件中。我已经成功地添加了所有必需的二进制文件和其他文件,但一个文件夹中的文件除外,我尝试了其他类似问题的解决方案,如,但没有效果。我也经历了这场比赛,但我想我仍然错过了一些东西。我尝试了使用相对路径和绝对路径进行添加。 我的项目结构如下: Project_Root_Folder model(folder) model.json file .h5 file other_data_folder
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函数来加载它
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')
这可确保路径根据应用程序是在其开发环境中还是在部署环境中而动态更改。