问题遇到的现象和发生背景
打包工具:pyinstaller
流程:
1.编写main.spec
2.运行pyinstaller main.spec
3.在生成的dist/AiLioBox 下通过dos窗口运行main.exe
问题相关代码,请勿粘贴截图
打包代码:
import torch
import os
os.environ["PYTORCH_JIT"] = "0"
if __name__ == '__main__':
print("hellowWorld!")
spec内容:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
files = ['ttt.py']
all_data = [
]
# 分析实例
a = Analysis(
# 可以在命令行中输入的Python脚本
files,
pathex=[],
binaries=[],
datas=all_data,
hiddenimports=[
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='main',
debug=True,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='AiLioBox')
运行结果及报错内容
Traceback (most recent call last):
File "ttt.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "torch\__init__.py", line 137, in <module>
raise err
OSError: [WinError 126] 找不到指定的模块。 Error loading "D:\Documents\A-Start\AiLioBox2.0\dist\AiLioBox\torch\lib\shm.dll" or one of its dependencies.
[16172] Failed to execute script 'ttt' due to unhandled exception!
我的解答思路和尝试过的方法
1.在hiddenimports中添加intel-openmp
2.在hiddenimports中添加pkg_resources.py2_warn
3.在binaries里添加(r'D:\ProgramData\Anaconda3\envs\ai_env\Lib\site-packages\torch\lib','.')
均为此报错
我想要达到的结果
正常输出hellowWorld!
谢谢大家