sys.modules strangeness

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
 
A

A. Lloyd Flanagan

Robin Becker said:
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.

Have you tried reload(ch1)? (see section 2.1, "Built-in Functions",
in the Python Library Reference.
 
R

Robin Becker

A. Lloyd Flanagan said:
Have you tried reload(ch1)? (see section 2.1, "Built-in Functions",
in the Python Library Reference.

I know that reload works. I was trying to restore the modules state to a
specific point as in general I didn't know where or which modules the apps could
import.

The original problem has gone away as I decided to exec the code files in a
specific namespace rather than import them. I am still curious why replacing the
current version of sys.modules with an earlier copy doesn't reset the modules list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top