No, this cannot be detected from the module code. After the first import, the module body is executed, and a new module object is inserted into sys.modules. Only after that the requested names are inserted into the namespace of the import module.
At subsequent import, the module body is not even executed. Therefore, if the module is first imported as
import module
and second time like
from module import name
he has no chance at all to do anything during the second import. In particular, it cannot check how it is imported.
source
share