微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 导入.so时导入语句的顺序是否重要?

尝试加载使用boost python编译的 python模块时,我收到以下导入错误.
ImportError: /path/to/library/libxml2.so.2: symbol gzopen64,version ZLIB_1.2.3.3 not defined in file libz.so.1 with link time reference

奇怪的是,如果这是要导入的非标准模块,我不会看到此错误.即如果我导入其他模块然后导入此模块,它将导致导入错误.不确定出现了什么问题或如何调试.

编辑:
要准确显示问题:

$python -c 'import json,libmYBOOST_PY_LIB' # DOES NOT WORK!!!
Traceback (most recent call last):
  File "<string>",line 1,in <module>
ImportError: path/to/xml_library/libxml2.so: symbol gzopen64,version ZLIB_1.2.3.3 not defined in file libz.so.1 with link time reference
$python -c 'import libmYBOOST_PY_LIB,json' # WORKS Now!!!
$

它不仅仅是json,在我的模块之前导入时,很少有其他模块也会导致同样的问题.例如.的urllib2

解决方法

导入语句的顺序很重要.

As documented in the python language reference

Once the name of the module is kNown (unless otherwise specified,the term “module” will refer to both packages and modules),searching for the module or package can begin. The first place checked is sys.modules,the cache of all modules that have been imported prevIoUsly. If the module is found there then it is used in step (2) of import.

任何模块都可以改变:

> sys.modules – 先前导入的所有模块的缓存
> sys.path – 模块的搜索路径

他们也可以改变导入钩子:

> sys.meta_path
> sys.path_hooks
> sys.path_importer_cache

导入挂钩可以让您从zip文件,任何类型的存档文件,网络等加载模块.

import libmYBOOST_PY_LIB

该语句将修改sys.modules,将其依赖项加载到模块缓存中.它也可以修改sys.path.实际上,框架(例如,boost,zope,django,请求……)与包含的电池/它们所依赖的模块的副本一起发货是非常常见的.

> django与json一起发货
>请求使用urllib3

要准确查看库将加载的内容,您可以使用:

python -v -c 'import libmYBOOST_PY_LIB'

原文地址:https://www.jb51.cc/c/110327.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐