本人环境配置:
RTX3060
Win10
cuda11.1
Python3.8
Pytorch1.7.1
本人需要将有导入torch包的python文件通过pyinstaller打包生成exe。
pyinstaller命令使用D参数,即生成一个dist文件夹,里面包含exe和需要调用的dll,命令行如下:
python -D your_python.py
显示结果如下:
Traceback (most recent call last):
File "" , line 12, in <module>
File "d:\users\admin\anaconda3\envs\pytorch171\lib\site-packages\matplotlib\backends\backend_webagg.py", line 29, in <module>
raise RuntimeError("The WebAgg backend requires Tornado.") from err
RuntimeError: The WebAgg backend requires Tornado.
47595 INFO: Matplotlib backend "WX": ignored
No module named 'wx'
48159 INFO: Matplotlib backend "WXAgg": ignored
No module named 'wx'
48719 INFO: Matplotlib backend "WXCairo": ignored
No module named 'wx'
49318 INFO: Matplotlib backend "agg": added
49874 INFO: Matplotlib backend "cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffi is installed
50580 INFO: Matplotlib backend "pdf": added
51267 INFO: Matplotlib backend "pgf": added
51910 INFO: Matplotlib backend "ps": added
52516 INFO: Matplotlib backend "svg": added
53199 INFO: Matplotlib backend "template": added
53536 INFO: Loading module hook 'hook-matplotlib.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
54109 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
54115 INFO: Loading module hook 'hook-numpy.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
54309 INFO: Import to be excluded not found: 'f2py'
54314 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
54318 INFO: Loading module hook 'hook-pandas.io.formats.style.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
55864 WARNING: Hidden import "jinja2" not found!
55864 INFO: Loading module hook 'hook-pandas.plotting.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
56071 INFO: Loading module hook 'hook-pandas.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
56484 INFO: Loading module hook 'hook-pickle.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
56489 INFO: Loading module hook 'hook-PIL.Image.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
56996 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
57001 INFO: Loading module hook 'hook-PIL.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
57017 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
57021 INFO: Loading module hook 'hook-pkg_resources.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
57954 INFO: Processing pre-safe import module hook win32com from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
58165 WARNING: Hidden import "pkg_resources.py2_warn" not found!
58165 WARNING: Hidden import "pkg_resources.markers" not found!
58171 INFO: Loading module hook 'hook-pytz.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58315 INFO: Loading module hook 'hook-scipy.linalg.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58316 INFO: Loading module hook 'hook-scipy.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58334 INFO: Loading module hook 'hook-scipy.sparse.csgraph.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58350 INFO: Loading module hook 'hook-scipy.spatial.transform.rotation.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58429 INFO: Loading module hook 'hook-scipy.special._ellip_harm_2.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58430 INFO: Loading module hook 'hook-scipy.special._ufuncs.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58430 INFO: Loading module hook 'hook-scipy.stats._stats.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
58431 INFO: Loading module hook 'hook-setuptools.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
59824 INFO: Loading module hook 'hook-sqlite3.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
59932 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
59933 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
60091 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
60091 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
60092 INFO: Loading module hook 'hook-xml.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
60092 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
60255 INFO: checking Tree
60309 INFO: checking Tree
60503 INFO: checking Tree
60523 INFO: Loading module hook 'hook-pythoncom.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
60888 INFO: Loading module hook 'hook-pywintypes.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
61183 INFO: Loading module hook 'hook-win32com.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
61650 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks'...
61761 INFO: Looking for ctypes DLLs
61885 INFO: Analyzing run-time hooks ...
61897 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
61919 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
61926 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
61936 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
61940 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_mplconfig.py'
61941 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\rthooks\\pyi_rth_certifi.py'
61944 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
61958 INFO: Including run-time hook 'd:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32api.py'
61994 INFO: Looking for dynamic libraries
65009 WARNING: lib not found: torch_python.dll dependency of d:\users\admin\anaconda3\envs\pytorch171\lib\site-packages\torch\_C.cp38-win_amd64.pyd
65463 INFO: Looking for eggs
65463 INFO: Using Python library d:\users\admin\anaconda3\envs\pytorch171\python38.dll
65463 INFO: Found binding redirects:
[]
65482 INFO: Warnings written to D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\warn-train_command_line_v4.txt
65757 INFO: Graph cross-reference written to D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\xref-train_command_line_v4.html
65948 INFO: checking PYZ
65986 INFO: Building because toc changed
65987 INFO: Building PYZ (ZlibArchive) D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\PYZ-00.pyz
68412 INFO: Building PYZ (ZlibArchive) D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\PYZ-00.pyz completed successfully.
68457 INFO: checking PKG
68479 INFO: Building because D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\PYZ-00.pyz changed
68479 INFO: Building PKG (CArchive) train_command_line_v4.pkg
68928 INFO: Building PKG (CArchive) train_command_line_v4.pkg completed successfully.
68929 INFO: Bootloader d:\users\admin\anaconda3\envs\pytorch171\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
68929 INFO: checking EXE
68935 INFO: Rebuilding EXE-00.toc because pkg is more recent
68935 INFO: Building EXE from EXE-00.toc
68936 INFO: Copying bootloader EXE to D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\train_command_line_v4.exe
68955 INFO: Copying icon to EXE
68955 INFO: Copying icons from ['d:\\users\\admin\\anaconda3\\envs\\pytorch171\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
68961 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
68961 INFO: Writing RT_ICON 1 resource with 3752 bytes
68961 INFO: Writing RT_ICON 2 resource with 2216 bytes
68961 INFO: Writing RT_ICON 3 resource with 1384 bytes
68962 INFO: Writing RT_ICON 4 resource with 37019 bytes
68962 INFO: Writing RT_ICON 5 resource with 9640 bytes
68962 INFO: Writing RT_ICON 6 resource with 4264 bytes
68962 INFO: Writing RT_ICON 7 resource with 1128 bytes
68969 INFO: Copying 0 resources to EXE
68969 INFO: Emedding manifest in EXE
68969 INFO: Updating manifest in D:\Python\yolov4-tiny-pytorch\build\train_command_line_v4\train_command_line_v4.exe
68970 INFO: Updating resource type 24 name 1 language 0
68974 INFO: Appending PKG archive to EXE
71284 INFO: Building EXE from EXE-00.toc completed successfully.
71293 INFO: checking COLLECT
71333 INFO: Building COLLECT COLLECT-00.toc
182417 INFO: Building COLLECT COLLECT-00.toc completed successfully.
最后的结果带有successfully就代表exe生成成功了。
打包显示成功,但是运行exe会报如下错误:
Traceback (most recent call last):
File "numpy\core\__init__.py", line 22, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\multiarray.py", line 12, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\overrides.py", line 7, in <module>
ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "yolov4_pt2onnx_v6.py", line 2, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "torch\__init__.py", line 190, in <module>
from torch._C import *
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\__init__.py", line 150, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\__init__.py", line 48, in <module>
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "my_path\my_exe.exe"
* The NumPy version is: "1.21.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
本人在网上查询了一堆资料,方法基本都是对numpy包升级或者降级,还有少数是设置环境变量等等,但是本人全部试了一遍,对我都没有用。
最终在一个Stackoverflow平台上看到有人提出可以将anaconda中的mkl开头的dll复制到numpy/core目录下。
于是我将dist目录下带有mkl的dll移动到numpy/core目录下,试着运行了下,竟然成功了!
参考资料:
ImportError: DLL load failed when importing Numpy installed in conda virtual environment