简体   繁体   English

导入同名的python模块和扩展模块

[英]Importing python modules and extension modules with the same name

Let's say I built a package with a Python module and an extension module in it, but with the identical name假设我构建了一个 package ,其中包含一个 Python 模块和一个扩展模块,但名称相同

mypackage
|
+-- __init__.py
|
+-- mymodule.py
|
+-- mymodule.cpython-39-x86_64-linux-gnu.so

I have found that when I do from mypackage import mymodule , it is the extension module that is being imported.我发现当我执行from mypackage import mymodule时,它是正在导入的扩展模块。 I wonder why this is the case?我想知道为什么会这样? Does extension modules take precedence during imports?在导入期间扩展模块是否优先? If so, could anyone point to where this behavior is documented?如果是这样,任何人都可以指出记录此行为的位置吗?

Check out this doc on imports, it includes the following blurb about importing modules with the same name:查看这个关于导入的文档,它包含以下关于导入同名模块的简介:

Tip: the search order is determined by the list of suffixes returned by the function imp.get_suffixes().提示:搜索顺序由 function imp.get_suffixes() 返回的后缀列表确定。 Usually the suffixes are searched in the following order: ".so", "module.so", ".py", ".pyc"通常按以下顺序搜索后缀:“.so”、“module.so”、“.py”、“.pyc”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM