I believe you misunderstood how __import__
works:
>>> from my_package import my_module
>>> my_module.function.__module__
'my_package.my_module'
>>> __import__(my_module.function.__module__)
<module 'my_package' from './my_package/__init__.py'>
From the documentation:
When the name variable is of the form
package.module
, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-emptyfromlist
argument is given, the module named by name is returned.
As you can see __import__
does not return the sub-module, but only the top package. If you have function
also defined at package level you will indeed have different references to it.
If you want to just load a module you should use importlib.import_module
instead of __import__
.
As to answer you actual question: AFAIK there is no way to import the same module, with the same name, twice without messing around with the importing mechanism. However, you could have a submodule of a package that is also available in the sys.path
, in this case you can import it twice using different names:
from some.package import submodule
import submodule as submodule2
print(submodule is submodule2) # False. They have *no* relationships.
This sometimes can cause problems with, e.g., pickle
. If you pickle something referenced by submodule
you cannot unpickle it using submodule2
as reference.
However this doesn't address the specific example you gave us, because using the __module__
attribute the import should return the correct module.