加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:45:15 所属栏目:百科 来源:网络整理
导读:尝试加载使用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 奇怪的是,如果这是要导入的非标准模块,我
尝试加载使用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'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读