运行PyInstaller生成的二进制文件时出现模块导入错误

时间:2016-08-03 22:45:42

标签: python python-2.7 pyinstaller

我在RHEL X64上使用PyInstaller尝试使用简单的hello.py时遇到错误。

Python 2.7.12是在/ opt / python

中安装的

编译输出:

[root@myrig CommandManager]# pyinstaller Hello.py
21 INFO: PyInstaller: 3.2
21 INFO: Python: 2.7.12
22 INFO: Platform: Linux-3.10.0-327.22.2.el7.x86_64-x86_64-with-redhat-7.2-Maipo
62 INFO: wrote /home/myuser/CommandManager/Hello.spec
66 INFO: UPX is not available.
107 INFO: Extending PYTHONPATH with paths
['/home/myuser/CommandManager', '/home/myuser/CommandManager']
107 INFO: checking Analysis
108 INFO: Building Analysis because out00-Analysis.toc is non existent
108 INFO: Initializing module dependency graph...
110 INFO: Initializing module graph hooks...
148 INFO: running Analysis out00-Analysis.toc
155 INFO: Caching module hooks...
158 INFO: Analyzing /home/myuser/CommandManager/Hello.py
160 INFO: Loading module hooks...
161 INFO: Loading module hook "hook-encodings.py"...
1493 INFO: Looking for ctypes DLLs
1493 INFO: Analyzing run-time hooks ...
1500 INFO: Looking for dynamic libraries
1801 INFO: Looking for eggs
1801 INFO: Python library not in binary depedencies. Doing additional searching...
1827 INFO: Using Python library /lib64/libpython2.7.so.1.0
1899 INFO: Warnings written to /home/myuser/CommandManager/build/Hello/warnHello.txt
1983 INFO: checking PYZ
1983 INFO: Building PYZ because out00-PYZ.toc is non existent
1983 INFO: Building PYZ (ZlibArchive) /home/myuser/CommandManager/build/Hello/out00-PYZ.pyz
2465 INFO: checking PKG
2465 INFO: Building PKG because out00-PKG.toc is non existent
2465 INFO: Building PKG (CArchive) out00-PKG.pkg
2648 INFO: Bootloader /opt/python/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
2648 INFO: checking EXE
2649 INFO: Building EXE because out00-EXE.toc is non existent
2649 INFO: Building EXE from out00-EXE.toc
2690 INFO: Appending archive to ELF section in EXE /home/myuser/CommandManager/build/Hello/Hello
2991 INFO: checking COLLECT
2992 INFO: Building COLLECT because out00-COLLECT.toc is non existent
2993 INFO: Building COLLECT out00-COLLECT.toc

Hello.py:

print("Hello")

这是我得到的错误:

mod is NULL - structTraceback (most recent call last):
  File "/opt/python/lib/python2.7/struct.py", line 1, in <module>
    from _struct import *
ImportError: /home/myuser/CommandManager/dist/Hello/_struct.so: undefined symbol: PyUnicodeUCS2_AsEncodedString
mod is NULL - pyimod02_archiveTraceback (most recent call last):
  File "/tmp/pip-build-xDjNbD/pyinstaller/PyInstaller/loader/pyimod02_archive.py", line 28, in <module>
ImportError: No module named struct
mod is NULL - pyimod03_importersTraceback (most recent call last):
  File "/tmp/pip-build-xDjNbD/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 24, in <module>
ImportError: No module named pyimod02_archive
Traceback (most recent call last):
  File "site-packages/PyInstaller/loader/pyiboot01_bootstrap.py", line 15, in <module>
ImportError: No module named pyimod03_importers
Failed to execute script pyiboot01_bootstrap

有什么可能导致这种情况的线索?

自动生成的Hello.spec文件如下所示:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['Hello.py'],
             pathex=['/home/myuser/CommandManager'],
             binaries=None,
             datas=None,
             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='Hello',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='Hello')

1 个答案:

答案 0 :(得分:1)

PyInstaller需要使用--enable-shared and LDFLAGS=-Wl,-rpath=<path to python lib>编译python。

就我而言:

  

./ configure --enable-shared --prefix = / opt / python   LDFLAGS = -Wl,-rpath = /选择/蟒/ lib中

补充参考: https://bugs.python.org/issue27685