本文介绍了运行 PyInstaller 生成的二进制文件时出现模块导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 RHEL X64 上尝试使用 PyInstaller 执行简单的 hello.py 时遇到错误.
I'm getting an error while trying to a simple hello.py using PyInstaller on RHEL X64.
Python 2.7.12 安装在/opt/python
Python 2.7.12 is alt installed in /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
有什么线索可能导致这种情况吗?
Any clue what may be causing this?
自动生成的 Hello.spec 文件如下所示:
The auto-generated Hello.spec file looks like this:
# -*- 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 和 LDFLAGS=-Wl,-rpath= 编译 python代码>.
PyInstaller needs python to be compiled with --enable-shared and LDFLAGS=-Wl,-rpath=<path to python lib>
.
就我而言:
./configure --enable-shared --prefix=/opt/pythonLDFLAGS=-Wl,-rpath=/opt/python/lib
补充参考:https://bugs.python.org/issue27685
这篇关于运行 PyInstaller 生成的二进制文件时出现模块导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!