我有一个简单的python脚本,可以在Ubuntu 14.04上导入couchbase模块:
$ cat test.py
from couchbase import Couchbase
print 'module _libcouchbase found'
从解释器运行正常:
$ python test.py
module _libcouchbase found
创建了一个可执行文件:
$ pyinstaller test.py
运行可执行文件会抛出无法导入_libcouchbase模块的错误:
$ ./dist/test/test
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/mycouch", line 28, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase", line 28, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase.user_constants", line 21, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase._bootstrap", line 34, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase.exceptions", line 18, in <module>
ImportError: No module named _libcouchbase
看起来很神秘。感谢有关为何发生这种情况的任何想法!
答案 0 :(得分:1)
尝试使用以下内容创建名为hook-couchbase.py
的文件:
import os
import glob
import itertools
try:
# PY_EXTENSION_SUFFIXES is unavailable in older versions
from PyInstaller.hooks.hookutils import PY_EXTENSION_SUFFIXES
except ImportError:
try:
from importlib.machinery import EXTENSION_SUFFIXES as PY_EXTENSION_SUFFIXES
except ImportError:
import imp
PY_EXTENSION_SUFFIXES = set([f[0] for f in imp.get_suffixes()
if f[2] == imp.C_EXTENSION])
def hook(mod):
module_directory = os.path.dirname(mod.__file__)
bundled = []
for libname, ext in itertools.product(('libcouchbase', '_libcouchbase'),
PY_EXTENSION_SUFFIXES):
bundled.extend(glob.glob(os.path.join(module_directory, libname + ext)))
for f in bundled:
name = os.path.join('couchbase', os.path.basename(f))
if hasattr(mod, 'pyinstaller_binaries'):
mod.pyinstaller_binaries.append((name, f, 'BINARY'))
else: # mod.pyinstaller_binaries is unavailable in older versions
mod.binaries.append((name, f, 'BINARY'))
return mod
构建时,提供放置文件的目录的路径作为--additional-hooks-dir
参数的值,如下所示:
--additional-hooks-dir=<path_to_directory_of_hook_file>