Python: Importing Module
This is expected behavior. When you import with from X import Y
, the module is still loaded and executed, as documented in the Language Reference. In fact, when you do
from fibo import fib
print("foo")
import fibo
will print This is a statement
, followed by foo
. The second import
doesn't print anything as the module is already cached.
Your second module will print This is a statement
followed by fibo
. The module knows its own name at load time.
Python has to load the whole module in order to import anything from it. Python imports the whole module into its module cache, but only the symbols you import are visible to you. (If you import a second time, it will not run; this is because the module is cached the first time it is imported.)