R
Robin Becker
We had some legacy applications that used import to get parts of documents in.
When run separately these worked fine, but failed when run as a single process
because they both imported ch1 (after jumping to their home dirs and placing
these on the path). Clearly the first to run used up ch1.
I have a simple test script below. It seems I cannot just restore the original
sys.modules and leave the modules to die, but actually need to del the relevant
entries. Are modules somehow cached somewhere? What's the right way to unload a
module (assuming I can remove non sys refs).
#timport.py start#######################
import sys, os
def d_b(d):
os.chdir(d)
cwd = os.getcwd()
sys.path.insert(0,os.getcwd())
import b
print 'Expecting %s:'%os.path.join(d,'b.py'),
b.run()
sys.path.remove(cwd)
os.chdir('..')
for d in 'a','c':
fn = os.path.join(d,'b.py')
f = open(fn,'r')
print 'The file %s is\n#######\n%s#######\n' % (fn,f.read())
f.close()
#this works
for d in 'a','c':
OK = sys.modules.keys()[:]
d_b(d)
for k in sys.modules.keys():
if k not in OK: del sys.modules[k]
#this doesn't
for d in 'a','c':
OM = sys.modules.copy()
d_b(d)
sys.modules = OM
#a/b.py##############################
def run():
print 'my file is', __file__, 'I am a\\b.py'
#c/b.py##############################
def run():
print 'my file is', __file__, 'I am c\\b.py'
#outputput###########################
The file a\b.py is
#######
def run():
print 'my file is', __file__, 'I am a\\b.py'
#######
The file c\b.py is
#######
def run():
print 'my file is', __file__, 'I am c\\b.py'
#######
Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\c\b.pyc I am c\b.py
Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
When run separately these worked fine, but failed when run as a single process
because they both imported ch1 (after jumping to their home dirs and placing
these on the path). Clearly the first to run used up ch1.
I have a simple test script below. It seems I cannot just restore the original
sys.modules and leave the modules to die, but actually need to del the relevant
entries. Are modules somehow cached somewhere? What's the right way to unload a
module (assuming I can remove non sys refs).
#timport.py start#######################
import sys, os
def d_b(d):
os.chdir(d)
cwd = os.getcwd()
sys.path.insert(0,os.getcwd())
import b
print 'Expecting %s:'%os.path.join(d,'b.py'),
b.run()
sys.path.remove(cwd)
os.chdir('..')
for d in 'a','c':
fn = os.path.join(d,'b.py')
f = open(fn,'r')
print 'The file %s is\n#######\n%s#######\n' % (fn,f.read())
f.close()
#this works
for d in 'a','c':
OK = sys.modules.keys()[:]
d_b(d)
for k in sys.modules.keys():
if k not in OK: del sys.modules[k]
#this doesn't
for d in 'a','c':
OM = sys.modules.copy()
d_b(d)
sys.modules = OM
#a/b.py##############################
def run():
print 'my file is', __file__, 'I am a\\b.py'
#c/b.py##############################
def run():
print 'my file is', __file__, 'I am c\\b.py'
#outputput###########################
The file a\b.py is
#######
def run():
print 'my file is', __file__, 'I am a\\b.py'
#######
The file c\b.py is
#######
def run():
print 'my file is', __file__, 'I am c\\b.py'
#######
Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\c\b.pyc I am c\b.py
Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py