By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

sys.modules strangeness

P: n/a
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
--
Robin Becker
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Robin Becker <ro***@reportlab.com> wrote in message news:<40**************@chamonix.reportlab.co.uk>.. .
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.
Jul 18 '05 #2

P: n/a
A. Lloyd Flanagan wrote:
Robin Becker <ro***@reportlab.com> wrote in message news:<40**************@chamonix.reportlab.co.uk>.. .
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.


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.
--
Robin Becker
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.