pyinsataller пропускает библиотеки при создании программ с использованием pyzmq

Я пытаюсь использовать pyinstaller для создания исполняемого файла моего рабочего клиента. Клиент использует ZMQ для связи с сервером. Но, к сожалению, pyinstaller, кажется, пропускает некоторые библиотеки при сборке zmq. Вот минимизированный пример на Win7 с

Anaconda 2 (python 2.7.13) 
zmq 16.0.2 
pyinstaller 3.3.dev0

Pyinstaller был установлен с использованием pip и хорошо работает для файлов python, которые не используют zmq. [обновление] Я обновил pyinstaller в соответствии с комментариями 9dogs, см. раздел "Информация". В последней версии я получаю дополнительное предупреждение каждый раз, когда звоню в pyinstaller

C:\zmqtest>pyinstaller -v
16 WARNING: Internal error: early pywin32 import was introduced
3.3.dev0+ga43a5d23

Содержание zmqtest.py файл:

import zmq
port = "5551"
context = zmq.Context()
client  = context.socket(zmq.REQ)
client.setsockopt(zmq.constants.LINGER, 1)
print 'connecting...'
client.connect ("tcp://localhost:%s" % port)
msgDict={}
msgDict['foo']='bar'
print 'send message...'
client.send_json(msgDict)
print 'recive message...'
msgDict = client.recv_json()

вывод pyinstaller:

   C:\zmqtest>pyinstaller zmqtest.py
36 WARNING: Internal error: early pywin32 import was introduced
459 INFO: PyInstaller: 3.3.dev0+ga43a5d23
459 INFO: Python: 2.7.13
459 INFO: Platform: Windows-7-6.1.7601-SP1
461 INFO: wrote C:\zmqtest\zmqtest.spec
463 INFO: UPX is not available.
466 INFO: Extending PYTHONPATH with paths
['C:\\zmqtest', 'C:\\zmqtest']
466 INFO: checking Analysis
466 INFO: Building Analysis because out00-Analysis.toc is non existent
466 INFO: Initializing module dependency graph...
473 INFO: Initializing module graph hooks...
612 INFO: running Analysis out00-Analysis.toc
625 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
  required by C:\Program Files\Anaconda2\python.exe
838 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
842 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
845 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
848 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.5054_none_acd24273e1d99eb3.manifest
851 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_acd388d7e1d8689f.manifest
1052 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_none ...
1052 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251.manifest
1055 INFO: Searching for file msvcr90.dll
1055 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251\msvcr90.dll
1056 INFO: Searching for file msvcp90.dll
1056 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251\msvcp90.dll
1056 INFO: Searching for file msvcm90.dll
1056 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251\msvcm90.dll
1282 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
1285 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
1286 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
1288 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.5054_none_acd24273e1d99eb3.manifest
1289 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_acd388d7e1d8689f.manifest
1291 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)
1411 INFO: Caching module hooks...
1417 INFO: Analyzing C:\zmqtest\zmqtest.py
5973 INFO: Processing pre-safe import module hook   _xmlplus
7472 INFO: Processing pre-find module path hook   distutils
9082 INFO: Processing pre-find module path hook   site
9085 INFO: site: retargeting to fake-dir 'C:\\Program Files\\Anaconda2\\lib\\site-packages\\PyInstaller\\fake-modules'
9125 INFO: Processing pre-safe import module hook   win32com
10780 INFO: Loading module hooks...
10782 INFO: Loading module hook "hook-distutils.py"...
10783 INFO: Loading module hook "hook-sysconfig.py"...
10786 INFO: Loading module hook "hook-xml.py"...
10947 INFO: Loading module hook "hook-zmq.py"...
14245 INFO: Excluding import 'zmq.libzmq'
14246 INFO:   Removing import of zmq.libzmq from module zmq
14246 INFO: Loading module hook "hook-pycparser.py"...
14499 INFO: Loading module hook "hook-httplib.py"...
14502 INFO: Loading module hook "hook-pkg_resources.py"...
15667 INFO: Loading module hook "hook-gevent.monkey.py"...
18127 INFO: Loading module hook "hook-pywintypes.py"...
18618 INFO: Loading module hook "hook-setuptools.py"...
25410 INFO: Loading module hook "hook-encodings.py"...
26375 INFO: Loading module hook "hook-numpy.core.py"...
26513 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
26516 INFO: Loading module hook "hook-win32com.py"...
26696 INFO: Loading module hook "hook-pythoncom.py"...
27161 INFO: Loading module hook "hook-pydoc.py"...
27166 INFO: Excluding import 'Tkinter'
27167 INFO:   Removing import of Tkinter from module pydoc
27168 INFO: Loading module hook "hook-_tkinter.py"...
27345 INFO: checking Tree
27347 INFO: Building Tree because out00-Tree.toc is non existent
27347 INFO: Building Tree out00-Tree.toc
27599 INFO: checking Tree
27599 INFO: Building Tree because out01-Tree.toc is non existent
27599 INFO: Building Tree out01-Tree.toc
28049 INFO: checking Tree
28049 INFO: Building Tree because out02-Tree.toc is non existent
28049 INFO: Building Tree out02-Tree.toc
28055 INFO: Looking for ctypes DLLs
28120 INFO: Analyzing run-time hooks ...
28125 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
28134 INFO: Including run-time hook 'pyi_rth_pkgres.py'
28141 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
28161 INFO: Looking for dynamic libraries
44992 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.4148_none_a551ff35e6a96bed.manifest
44996 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.5054_none_a5535ca9e6a81b96.manifest
45000 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_a554a30de6a6e582.manifest
45223 INFO: Searching for assembly amd64_Microsoft.VC90.MFC_1fc8b3b9a1e18e3b_9.0.30729.6161_none ...
45223 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_044aad0bab1eb146.manifest
45226 INFO: Searching for file mfc90.dll
45226 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_044aad0bab1eb146\mfc90.dll
45226 INFO: Searching for file mfc90u.dll
45226 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_044aad0bab1eb146\mfc90u.dll
45227 INFO: Searching for file mfcm90.dll
45227 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_044aad0bab1eb146\mfcm90.dll
45227 INFO: Searching for file mfcm90u.dll
45227 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_044aad0bab1eb146\mfcm90u.dll
45434 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.4148_none_a551ff35e6a96bed.manifest
45437 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.5054_none_a5535ca9e6a81b96.manifest
45438 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.mfc_1fc8b3b9a1e18e3b_9.0.30729.6161_none_a554a30de6a6e582.manifest
45440 INFO: Adding redirect Microsoft.VC90.MFC version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)
45684 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\message.pyd
45785 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\_device.pyd
45884 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\utils.pyd
45983 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\socket.pyd
46082 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\_poll.pyd
46190 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\context.pyd
46302 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\error.pyd
46404 WARNING: lib not found: libzmq.pyd dependency of C:\Program Files\Anaconda2\lib\site-packages\zmq\backend\cython\_version.pyd
46588 INFO: Looking for eggs
46589 INFO: Using Python library C:\Program Files\Anaconda2\python27.dll
46589 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'amd64', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 6161), publicKeyToken
=u'1fc8b3b9a1e18e3b'), BindingRedirect(name=u'Microsoft.VC90.MFC', language=None, arch=u'amd64', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729,
 6161), publicKeyToken=u'1fc8b3b9a1e18e3b')]
46601 INFO: Warnings written to C:\zmqtest\build\zmqtest\warnzmqtest.txt
46759 INFO: checking PYZ
46760 INFO: Building PYZ because out00-PYZ.toc is non existent
46760 INFO: Building PYZ (ZlibArchive) C:\zmqtest\build\zmqtest\out00-PYZ.pyz
48335 INFO: Building PYZ (ZlibArchive) C:\zmqtest\build\zmqtest\out00-PYZ.pyz completed successfully.
48569 INFO: checking PKG
48571 INFO: Building PKG because out00-PKG.toc is non existent
48571 INFO: Building PKG (CArchive) out00-PKG.pkg
48609 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
48615 INFO: Bootloader C:\Program Files\Anaconda2\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
48617 INFO: checking EXE
48617 INFO: Building EXE because out00-EXE.toc is non existent
48618 INFO: Building EXE from out00-EXE.toc
48618 INFO: Appending archive to EXE C:\zmqtest\build\zmqtest\zmqtest.exe
48634 INFO: Building EXE from out00-EXE.toc completed successfully.
48644 INFO: checking COLLECT
48644 INFO: Building COLLECT because out00-COLLECT.toc is non existent
48644 INFO: Building COLLECT out00-COLLECT.toc
48673 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 6161)
51664 INFO: Building COLLECT out00-COLLECT.toc completed successfully.

Содержание .spec файл:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['zmqtest.py'],
             pathex=['C:\\zmqtest'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             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,
          exclude_binaries=True,
          name='zmqtest',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='zmqtest')

И ошибка, когда я пытаюсь запустить сгенерированный exe:

C:\zmqtest\dist\zmqtest>zmqtest.exe
Traceback (most recent call last):
  File "zmqtest.py", line 1, in <module>
  File "C:\Program Files\Anaconda2\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
    exec(bytecode, module.__dict__)
  File "site-packages\zmq\__init__.py", line 34, in <module>
  File "C:\Program Files\Anaconda2\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
    exec(bytecode, module.__dict__)
  File "site-packages\zmq\backend\__init__.py", line 40, in <module>
  File "site-packages\zmq\backend\__init__.py", line 27, in <module>
  File "site-packages\zmq\backend\select.py", line 26, in select_backend
  File "C:\Program Files\Anaconda2\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
    exec(bytecode, module.__dict__)
  File "site-packages\zmq\backend\cython\__init__.py", line 6, in <module>
  File "C:\Program Files\Anaconda2\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 687, in load_module
    module = imp.load_module(fullname, fp, filename, ext_tuple)
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.
[4836] Failed to execute script zmqtest

Есть идеи, как запустить zmq с pyinstaller?

[Информация]

для всех, кто хочет следовать инструкции 9dogs и установить последнюю версию pyinstaller с

pip install git+https://github.com/pyinstaller/pyinstaller.git

убедитесь, что у вас установлен git.

1 ответ

Решение

Итак, я понял, что коренная причина в том, что zmq.backend.select импортирует несколько модулей с __import__ который скрыт для pyinstaller. Динамически загружаемые модули можно идентифицировать, вызвав тестовый файл python в подробном режиме:

python -v zmqtest.py

Кроме того, папка, содержащая libzmq.pyd не найден, поэтому его путь должен быть добавлен вручную. Полный pyinstaller вызов, который работает для меня, теперь выглядит так:

pyinstaller  --noconfirm --log-level=WARN ^
             --hidden-import=zmq.backend ^
             --hidden-import=zmq.backend.cython ^
             --hidden-import=zmq.backend.cffi ^
             --hidden-import=zmq.error  ^
             --hidden-import=zmq.sugar ^
             --hidden-import=zmq.utils ^
             --paths="C:\Program Files\Anaconda2\pkgs\pyzmq-16.0.2-py27_0\Lib\site-packages\zmq" ^
             zmqtest.py
Другие вопросы по тегам