简体   繁体   中英

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

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. 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(). Usually the suffixes are searched in the following order: ".so", "module.so", ".py", ".pyc"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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