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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/c/110327.html

猜你在找的C&C++相关文章