0

概要

YOLOv5のリポジトリからダウンロードしてきたdetect.pyをPyinstallerでexe化することを考えています
exeにすることはできますが、実行しようとすると以下のエラーが出てしまいます

Traceback (most recent call last):
  File "detect.py", line 37, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'
[29508] Failed to execute script detect

どのようにすればエラーを解決してYOLOv5をexeで実行できるでしょうか?

詳細

環境

Python: 3.7.5
Pyinstaller: 3.6
OS: Windowes10

スクリプト

Pyinstallerでexeを作成したときのメッセージは下記の通りです

❯ pyinstaller .\detect.spec --hiddenimport=torch
284 INFO: PyInstaller: 3.6
285 INFO: Python: 3.7.4 (conda)
286 INFO: Platform: Windows-10-10.0.19041-SP0
297 INFO: UPX is not available.
325 INFO: Extending PYTHONPATH with paths
['D:\\tmp\\pys\\yolov5', 'D:\\tmp\\pys\\yolov5']
326 INFO: checking Analysis
326 INFO: Building Analysis because Analysis-00.toc is non existent
327 INFO: Initializing module dependency graph...
338 INFO: Caching module graph hooks...
351 INFO: Analyzing base_library.zip ...
14995 INFO: Caching module dependency graph...
15287 INFO: running Analysis Analysis-00.toc
15321 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\program\anaconda\python.exe
17218 INFO: Analyzing detect.py
24688 INFO: Processing pre-find module path hook   distutils
24690 INFO: distutils: retargeting to non-venv dir 'd:\\program\\anaconda\\lib'
25180 INFO: Processing pre-find module path hook   site
25181 INFO: site: retargeting to fake-dir 'd:\\program\\anaconda\\lib\\site-packages\\PyInstaller\\fake-modules'
31617 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
65258 INFO: Processing pre-safe import module hook   six.moves
84436 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
240581 INFO: Processing pre-safe import module hook   win32com
279374 INFO: Processing module hooks...
279374 INFO: Loading module hook "hook-babel.py"...
279670 INFO: Loading module hook "hook-bokeh.py"...
280134 INFO: Loading module hook "hook-certifi.py"...
280138 INFO: Loading module hook "hook-Crypto.py"...
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'Crypto.Math'
280644 INFO: Loading module hook "hook-cryptography.py"...
281578 INFO: Loading module hook "hook-cv2.py"...
281579 INFO: Loading module hook "hook-cytoolz.itertoolz.py"...
281602 INFO: Loading module hook "hook-distutils.py"...
281604 INFO: Loading module hook "hook-docutils.py"...
287944 INFO: Loading module hook "hook-encodings.py"...
288196 INFO: Loading module hook "hook-h5py.py"...
288202 INFO: Loading module hook "hook-importlib_metadata.py"...
288208 INFO: Loading module hook "hook-IPython.py"...
289128 INFO: Excluding import 'PyQt5'
289139 INFO:   Removing import of PyQt5.QtWidgets from module IPython.external.qt_loaders
289139 INFO:   Removing import of PyQt5 from module IPython.external.qt_loaders
289139 INFO:   Removing import of PyQt5.QtCore from module IPython.external.qt_loaders
289140 INFO:   Removing import of PyQt5.QtGui from module IPython.external.qt_loaders
289140 INFO:   Removing import of PyQt5.QtSvg from module IPython.external.qt_loaders
289141 INFO: Excluding import 'gtk'
289152 INFO: Excluding import 'PySide'
289162 INFO:   Removing import of PySide from module IPython.external.qt_loaders
289163 INFO: Excluding import 'matplotlib'
289172 INFO:   Removing import of matplotlib from module IPython.core.pylabtools
289173 INFO:   Removing import of matplotlib.pyplot from module IPython.core.pylabtools
289173 INFO:   Removing import of matplotlib.figure from module IPython.core.pylabtools
289173 INFO:   Removing import of matplotlib._pylab_helpers from module IPython.core.pylabtools
289175 INFO: Excluding import 'tkinter'
289185 INFO:   Removing import of tkinter from module IPython.lib.clipboard
289187 INFO: Excluding import 'PyQt4'
289198 INFO:   Removing import of PyQt4 from module IPython.external.qt_loaders
289199 INFO: Loading module hook "hook-jedi.py"...
289723 INFO: Loading module hook "hook-jinja2.py"...
289769 INFO: Loading module hook "hook-jsonschema.py"...
289779 INFO: Loading module hook "hook-lib2to3.py"...
289785 INFO: Loading module hook "hook-llvmlite.py"...
289799 INFO: Loading module hook "hook-lxml.etree.py"...
289801 INFO: Loading module hook "hook-matplotlib.backends.py"...
295549 INFO:   Matplotlib backend "GTK3Agg": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
296349 INFO:   Matplotlib backend "GTK3Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
297399 INFO:   Matplotlib backend "MacOSX": ignored
    cannot import name '_macosx' from 'matplotlib.backends' (d:\program\anaconda\lib\site-packages\matplotlib\backends\__init__.py)
299786 INFO:   Matplotlib backend "nbAgg": added
302090 INFO:   Matplotlib backend "Qt4Agg": added
302867 INFO:   Matplotlib backend "Qt4Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
303820 INFO:   Matplotlib backend "Qt5Agg": added
304638 INFO:   Matplotlib backend "Qt5Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
305898 INFO:   Matplotlib backend "TkAgg": added
306993 INFO:   Matplotlib backend "TkCairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
308099 INFO:   Matplotlib backend "WebAgg": added
309109 INFO:   Matplotlib backend "WX": ignored
    No module named 'wx'
309911 INFO:   Matplotlib backend "WXAgg": ignored
    No module named 'wx'
310697 INFO:   Matplotlib backend "WXCairo": ignored
    No module named 'wx'
311528 INFO:   Matplotlib backend "agg": added
312327 INFO:   Matplotlib backend "cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
313423 INFO:   Matplotlib backend "pdf": added
314470 INFO:   Matplotlib backend "pgf": added
315327 INFO:   Matplotlib backend "ps": added
316177 INFO:   Matplotlib backend "svg": added
317185 INFO:   Matplotlib backend "template": added
318467 INFO: Loading module hook "hook-matplotlib.py"...
319267 INFO: Loading module hook "hook-nbconvert.py"...
319314 INFO: Loading module hook "hook-nbformat.py"...
319328 INFO: Loading module hook "hook-notebook.py"...
321893 INFO: Loading module hook "hook-numba.py"...
321904 INFO: Excluding import 'IPython'
321915 INFO:   Removing import of IPython.paths from module numba.caching
321916 INFO:   Removing import of IPython.utils.path from module numba.caching
321922 INFO: Excluding import 'scipy'
321938 INFO:   Removing import of scipy.linalg.cython_lapack from module numba.targets.linalg
321939 INFO:   Removing import of scipy.linalg.cython_blas from module numba.targets.linalg
321940 INFO: Loading module hook "hook-numpy.core.py"...
322191 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
322203 INFO: Loading module hook "hook-numpy.py"...
322205 INFO: Loading module hook "hook-openpyxl.py"...
322224 INFO: Loading module hook "hook-pandas.py"...
330953 INFO: Loading module hook "hook-patsy.py"...
330955 INFO: Loading module hook "hook-PIL.Image.py"...
332503 INFO: Loading module hook "hook-PIL.py"...
332514 INFO: Excluding import 'PyQt5'
332522 INFO:   Removing import of PyQt5.QtCore from module PIL.ImageQt
332522 INFO:   Removing import of PyQt5.QtGui from module PIL.ImageQt
332524 INFO: Excluding import 'PyQt4'
332532 INFO:   Removing import of PyQt4 from module PIL.ImageQt
332533 INFO: Excluding import 'PySide'
332541 INFO:   Removing import of PySide from module PIL.ImageQt
332542 INFO: Excluding import 'tkinter'
332552 INFO:   Removing import of tkinter from module PIL.ImageTk
332553 INFO: Import to be excluded not found: 'FixTk'
332553 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
332562 INFO: Excluding import 'tkinter'
332568 INFO: Import to be excluded not found: 'FixTk'
332568 INFO: Loading module hook "hook-pkg_resources.py"...
334527 INFO: Excluding import '__main__'
334534 INFO:   Removing import of __main__ from module pkg_resources
334535 INFO: Loading module hook "hook-pycparser.py"...
334537 INFO: Loading module hook "hook-pydoc.py"...
334538 INFO: Loading module hook "hook-pygments.py"...
340228 INFO: Loading module hook "hook-PyQt5.py"...
341244 WARNING: Hidden import "PyQt5.sip" not found!
341245 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
341754 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
342197 INFO: Loading module hook "hook-PyQt5.QtSvg.py"...
342887 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
343340 INFO: Loading module hook "hook-pytest.py"...
346078 INFO: Loading module hook "hook-pythoncom.py"...
347369 INFO: Loading module hook "hook-pytz.py"...
347613 INFO: Loading module hook "hook-pywintypes.py"...
348893 INFO: Loading module hook "hook-scipy.linalg.py"...
348895 INFO: Loading module hook "hook-scipy.py"...
348900 INFO: Loading module hook "hook-scipy.sparse.csgraph.py"...
348922 INFO: Loading module hook "hook-scipy.special._ellip_harm_2.py"...
348923 INFO: Loading module hook "hook-scipy.special._ufuncs.py"...
348925 INFO: Loading module hook "hook-setuptools.py"...
350829 INFO: Loading module hook "hook-sphinx.py"...
363762 INFO: Loading module hook "hook-sqlalchemy.py"...
365028 INFO:   Found 4 sqlalchemy hidden imports
365033 WARNING: Hidden import "MySQLdb" not found!
365037 WARNING: Hidden import "psycopg2" not found!
370685 WARNING: Hidden import "sqlalchemy.sql.functions.func" not found!
370696 INFO: Import to be excluded not found: 'sqlalchemy.testing'
370696 INFO: Loading module hook "hook-sqlite3.py"...
370913 INFO: Loading module hook "hook-sysconfig.py"...
370915 INFO: Loading module hook "hook-tables.py"...
370918 INFO: Loading module hook "hook-win32com.py"...
371318 INFO: Loading module hook "hook-xml.dom.domreg.py"...
371320 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
371321 INFO: Loading module hook "hook-xml.py"...
371323 INFO: Loading module hook "hook-zmq.py"...
383434 WARNING: Hidden import "C1083:" not found!
383438 WARNING: Hidden import "d:\program\anaconda\lib\site-packages\zmq\backend\cffi\__pycache__\_cffi_ext.c(213):" not found!
383672 WARNING: Hidden import "or" not found!
383675 WARNING: Hidden import "directory" not found!
384030 WARNING: Hidden import "file" not found!
384035 WARNING: Hidden import "fatal" not found!
384040 WARNING: Hidden import "error" not found!
384044 WARNING: Hidden import "_cffi_ext.c" not found!
384049 WARNING: Hidden import "ファイルを開けません。'sys/un.h':No" not found!
384256 WARNING: Hidden import "such" not found!
384256 INFO: Loading module hook "hook-_tkinter.py"...
384699 INFO: checking Tree
384699 INFO: Building Tree because Tree-00.toc is non existent
384700 INFO: Building Tree Tree-00.toc
384782 INFO: checking Tree
384782 INFO: Building Tree because Tree-01.toc is non existent
384783 INFO: Building Tree Tree-01.toc
384797 INFO: Loading module hook "hook-gevent.py"...
385465 INFO: Determining a mapping of distributions to packages...
506593 WARNING: Unable to find package for requirement greenlet from package gevent.
506594 INFO: Packages required by gevent:
['cffi']
514929 INFO: Looking for ctypes DLLs
515808 INFO: Analyzing run-time hooks ...
515873 INFO: Including run-time hook 'pyi_rth_traitlets.py'
515889 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
515908 INFO: Including run-time hook 'pyi_rth_pkgres.py'
515924 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
515939 INFO: Including run-time hook 'pyi_rth__tkinter.py'
515953 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
515966 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
515980 INFO: Including run-time hook 'pyi_rth_mpldata.py'
515992 INFO: Including run-time hook 'pyi_rth_certifi.py'
516142 INFO: Looking for dynamic libraries
517334 WARNING: lib not found: msmpi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_msmpi_lp64.dll
517843 WARNING: lib not found: mpich2mpi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_mpich2_ilp64.dll
517926 WARNING: lib not found: msmpi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_msmpi_ilp64.dll
518616 WARNING: lib not found: impi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_intelmpi_lp64.dll
521834 WARNING: lib not found: pgc14.dll dependency of d:\program\anaconda\Library\bin\mkl_pgi_thread.dll
521857 WARNING: lib not found: pgf90.dll dependency of d:\program\anaconda\Library\bin\mkl_pgi_thread.dll
521873 WARNING: lib not found: pgf90rtl.dll dependency of d:\program\anaconda\Library\bin\mkl_pgi_thread.dll
524451 WARNING: lib not found: mpich2mpi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_mpich2_lp64.dll
526839 WARNING: lib not found: impi.dll dependency of d:\program\anaconda\Library\bin\mkl_blacs_intelmpi_ilp64.dll
539960 INFO: Looking for eggs
539961 INFO: Using Python library d:\program\anaconda\python37.dll
539961 INFO: Found binding redirects:
[]
540043 INFO: Warnings written to D:\tmp\pys\yolov5\build\detect\warn-detect.txt
541542 INFO: Graph cross-reference written to D:\tmp\pys\yolov5\build\detect\xref-detect.html
542237 INFO: checking PYZ
542238 INFO: Building PYZ because PYZ-00.toc is non existent
542238 INFO: Building PYZ (ZlibArchive) D:\tmp\pys\yolov5\build\detect\PYZ-00.pyz
557688 INFO: Building PYZ (ZlibArchive) D:\tmp\pys\yolov5\build\detect\PYZ-00.pyz completed successfully.
557968 INFO: checking PKG
557969 INFO: Building PKG because PKG-00.toc is non existent
557969 INFO: Building PKG (CArchive) PKG-00.pkg
812775 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
813207 INFO: Bootloader d:\program\anaconda\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
813207 INFO: checking EXE
813208 INFO: Building EXE because EXE-00.toc is non existent
813208 INFO: Building EXE from EXE-00.toc
813209 INFO: Appending archive to EXE D:\tmp\pys\yolov5\dist\detect.exe
814031 INFO: Building EXE from EXE-00.toc completed successfully.

exe化対象のspecファイルは以下の通りです

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None
import sys
sys.setrecursionlimit(5000)

a = Analysis(['detect.py'],
             pathex=['D:\\tmp\\pys\\yolov5'],
             binaries=[],
             datas=[],
             hiddenimports=['torch'],
             hookspath=[],
             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,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='detect',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

onefileとhidden-importの指定の有無はそれぞれ試してみましたが、同じ結果でした
pythonのままであればdetect.pyを実行できることは確認済みです

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。