I would like to know if the following behavior is expected or a bug. I'm using CPython2.7
Create a file x.py
def funcA():
print "funcA of x.py"
def funcB():
print "funcB of x.py"
Create a file y.py
def funcB():
print "funcB of y.py"
Create a file test.py
import sys, imp
# load x.py as fff
m = imp.load_source('fff', 'x.py')
print dir(m)
print sys.modules.get('fff')
# load y.py as fff
m = imp.load_source('fff', 'y.py')
print dir(m)
print sys.modules.get('fff')
# import and exec func
import fff
fff.funcA()
fff.funcB()
print dir(fff)
The result
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'funcA', 'funcB']
<module 'fff' from 'x.py'>
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'funcA', 'funcB']
<module 'fff' from 'y.py'>
funcA of x.py
funcB of y.py
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'funcA', 'funcB']
My expectation was that the 2nd imp.load_source
would completely replace the module x.py with y.py. In fact sys.modules.get('fff')
shows <module 'fff' from 'y.py'>
but the resulting module was like a mix of x.py and y.py, and the latter has the precedence.
Is this expected or a bug?
EDIT: my test code had a typo. updated the result.