imp主要有两种用法
(1)imp.find_module(name[,path])
如果path为空,则按照sys.path路径搜索模块名, 返回三元组(file, pathname, description).file为刚打开的模块文件, pathname为模块的路径, description为imp.get_suffixes()返回的元组.
如果模块为包,file返回None, pathname为包路径, description返回的type为PKG_DIRECTORY.
find_module不会处理层次结构的模块名(带’.’号的模块名module.name1.name2).
“path”必须是一个列表.
(2)imp.load_module(name, file, pathname, description)
加载一个被find_module找到的模块. 如果模块已经被加载, 等同于reload().
当模块是包或者不从文件加载时, file和pathname可以是None和”.
成功加载后返回一个模块对象,否则抛出 ImportError异常.
需要自己关闭file,最好用try…finally…
功能代码如下:
def swig_import_helper(soFilePath="/home/biyuzhe"): #self,
import imp
fp = None
try:
fp, pathname, description = imp.find_module('_Ph**alizer', [soFilePath])
except ImportError:
import _PhoneNormalizer
return _PhoneNormalizer
if fp is not None:
try:
_mod = imp.load_module('_Ph**alizer', fp, pathname, description)
finally:
fp.close()
return _mod
else:
return
问题与解决方案:
(一)在使用_Pho**lizer.so包时报错,缺少依赖
/home/raini/ideaProject/Evernalysis/EveecSpark/_Pho**mazer.so
Traceback (most recent call last):
File "/home/raini/ideaProject/EverSecAnalysis/EverSecSpark/sparkTest.py", line 31, in
_PhoneNormalizer = swig_import_helper()
File "/home/raini/ideaProject/EverSecAnalysis/EverSecSpark/sparkTest.py", line 23, in swig_import_helper
_mod = imp.load_module('_PhoneNormalizer', fp, pathname, description)
File "/usr/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
File "", line 693, in _load
File "", line 666, in _load_unlocked
File "", line 577, in module_from_spec
File "", line 906, in create_module
File "", line 222, in _call_with_frames_removed
ImportError: libmysqlclient.so.16: cannot open shared object file: No such file or directory
[Finished in 0.183s]
解决(尝试1):
下载该依赖,放入指定路径
# wget -O /usr/local/mysql/lib/libmysqlclient.so.16 http://files.directadmin.com/services/debian_5.0_64/libmysqlclient.so.16
# chmod 755 /usr/local/mysql/lib/libmysqlclient.so.16
# ldconfig
raini@biyuzhe:~/EverSecSpark$ sudo mkdir -p /usr/local/mysql/lib
raini@biyuzhe:~/EverSecSpark$ sudo mv libmysqlclient.so.16 /usr/local/mysql/lib/libmysqlclient.so.16
raini@biyuzhe:~/EverSecSpark$ sudo chmod 755 /usr/local/mysql/lib/libmysqlclient.so.16
raini@biyuzhe:~/EverSecSpark$ sudo ldconfig
新问题:
$ sudo ldconfig (出现的问题):
/sbin/ldconfig.real: /usr/lib/nvidia-384/libEGL.so.1 不是符号连接
/sbin/ldconfig.real: /usr/lib32/nvidia-384/libEGL.so.1 不是符号连接
原因:
系统找的是一个符号连接,而不是一个文件。
解决方法:
sudo mv /usr/lib/nvidia-384/libEGL.so.1 /usr/lib/nvidia-384/libEGL.so.1.org
sudo mv /usr/lib32/nvidia-384/libEGL.so.1 /usr/lib32/nvidia-384/libEGL.so.1.org
sudo ln -s /usr/lib/nvidia-384/libEGL.so.375.39 /usr/lib/nvidia-384/libEGL.so.1
sudo ln -s /usr/lib32/nvidia-384/libEGL.so.375.39 /usr/lib32/nvidia-384/libEGL.so.1
最后再:
raini@biyuzhe:~$ sudo ldconfig
(新问题解决,但是python代码还是报错):
ImportError: libmysqlclient.so.16: cannot open shared object file: No such file or directory
(2)尝试别的方法:
解决:
找到该文件位置
raini@biyuzhe:~/ideaProject/EverSecAnalysis/EverSecSpark$ locate libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.7
raini@biyuzhe:~/ideaProject/EverSecAnalysis/EverSecSpark$ sudo cp libmysqlclient.so.16 /usr/lib/x86_64-linux-gnu/
raini@biyuzhe:~/ideaProject/EverSecAnalysis/EverSecSpark$ sudo ldconfig
结果:
错误变了
>>> fp, pathname, description = imp.find_module('_Pho**malizer', [dirname])
>>> _mod = imp.load_module('_PhoneNormalizer', fp, pathname, description)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
File "", line 693, in _load
File "", line 666, in _load_unlocked
File "", line 577, in module_from_spec
File "", line 906, in create_module
File "", line 222, in _call_with_frames_removed
ImportError: /home/raini/ideaProject/EverSecAnalysis/EverSecSpark/_Pho**alizer.so: undefined symbol: _Py_ZeroStruct
出错原因:
当Python模块中存在版本不匹配时,此错误消息是典型的。
它可以像_PhoneNormalizer.so一样被编译并链接到Python 2.7,而我写的该程序是针对Python3.5编译的。
很难说出原因是什么,但是您可以尝试在与您的程序版本匹配的python环境中进行make make,看看是否有效。
解决方法:
因为我的“_PhoneNormalizer.so”文件是python2.6的环境内编译的,而我目前的编程环境是python3.6,因此出错。
解决方法是只能使用Python2。(并且需要/usr/lib64下有 libmysqlclient.so.16 )
# 集群机器-实验:
查看有没有装libmysqlclient.so.16:find / -name libmysqlclient.so.16
如果装了,就
ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16
注:/usr/local/mysql/lib/mysql/为libmysqlclient.so.16的路径。在/usr/lib/下建立个软链接。
^_^ 试了管用~
1.方法一:
下载libmysqlclient.so.16,放在/usr/lib下,如果是64位,则放在/usr/lib64下
2.方法二:
采用: MySQL-shared-compat 的rpm包。
运行命令:#rpm -qp --provides MySQL-shared-compat-5.1.47-1.rhel5.x86_64.rpm
MySQL-shared
libmysqlclient.so.12()(64bit)
libmysqlclient.so.14()(64bit)
libmysqlclient.so.14(libmysqlclient_14)(64bit)
libmysqlclient.so.15()(64bit)
libmysqlclient.so.15(libmysqlclient_15)(64bit)
libmysqlclient.so.16()(64bit)
libmysqlclient.so.16(libmysqlclient_16)(64bit)
libmysqlclient_r.so.12()(64bit)
libmysqlclient_r.so.14()(64bit)
libmysqlclient_r.so.14(libmysqlclient_14)(64bit)
libmysqlclient_r.so.15()(64bit)
libmysqlclient_r.so.15(libmysqlclient_15)(64bit)
libmysqlclient_r.so.16()(64bit)
libmysqlclient_r.so.16(libmysqlclient_16)(64bit)
MySQL-shared-compat = 5.1.47-1.rhel5
从执行结果上看,此方法可以彻底解决关于类似 libmysqlclient.so 的问题。不用出现一个不兼容问题,就去解决一个。
下载地址:http://dev.mysql.com/downloads/mysql/5.1.html
下载安装包名:MySQL-shared-compat-5.1.47-1.rhel5.x86_64
执行命令安装:#rpm -ivh MySQL-shared-compat-5.1.47-1.rhel5.x86_64
最终解决:
[root@node1 home]# ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib64/libmysqlclient.so.16