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

标签 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

你好.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')

最佳答案

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

就我而言:

./configure --enable-shared --prefix=/opt/python LDFLAGS=-Wl,-rpath=/opt/python/lib

附加引用: https://bugs.python.org/issue27685

关于python - 运行 PyInstaller 生成的二进制文件时出现模块导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754847/

相关文章:

python - Pandas Join 未提供确切的结果

Python Pyhive 模块无法导入名称配置单元

python - 启动 Popen 命令后脚本退出

python - 使用 PyInstaller 检查的问题;可以获得类的来源,但不能获得函数

python - 检索作为 POST 响应提供的重定向 URL 中的 OAuth 代码

python - 'collections.defaultdict' 对象没有属性 'append'

python - 带有brew的OS X上的virtualenv中的PyGame?

python-2.7 - 在wave中显示和编辑声音,python 2.7

python - 调试 PyInstaller 打包的 Python/PyQt 应用程序的选项?

python - Windows 的 Python 服务有几个问题