Tetheras
Tetheras

Reputation: 53

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?

Upvotes: 0

Views: 281

Answers (1)

Bernstern
Bernstern

Reputation: 201

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"

Upvotes: 1

Related Questions