sys.modules strangeness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robin Becker

    sys.modules strangeness

    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.jo in(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.key s()[:]
    d_b(d)
    for k in sys.modules.key s():
    if k not in OK: del sys.modules[k]

    #this doesn't
    for d in 'a','c':
    OM = sys.modules.cop y()
    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
  • A. Lloyd Flanagan

    #2
    Re: sys.modules strangeness

    Robin Becker <robin@reportla b.com> wrote in message news:<408FF0B1. 5090505@chamoni x.reportlab.co. uk>...[color=blue]
    > 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.[/color]

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

    Comment

    • Robin Becker

      #3
      Re: sys.modules strangeness

      A. Lloyd Flanagan wrote:[color=blue]
      > Robin Becker <robin@reportla b.com> wrote in message news:<408FF0B1. 5090505@chamoni x.reportlab.co. uk>...
      >[color=green]
      >>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.[/color]
      >
      >
      > Have you tried reload(ch1)? (see section 2.1, "Built-in Functions",
      > in the Python Library Reference.[/color]

      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

      Comment

      Working...