sys.modules strangeness

Discussion in 'Python' started by Robin Becker, Apr 28, 2004.

  1. Robin Becker

    Robin Becker Guest

    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
     
    Robin Becker, Apr 28, 2004
    #1
    1. Advertising

  2. Robin Becker <> wrote in message news:<>...
    > 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.
     
    A. Lloyd Flanagan, Apr 29, 2004
    #2
    1. Advertising

  3. Robin Becker

    Robin Becker Guest

    A. Lloyd Flanagan wrote:
    > Robin Becker <> wrote in message news:<>...
    >
    >>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
     
    Robin Becker, Apr 29, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald Meier
    Replies:
    2
    Views:
    1,578
    Keith S.
    Oct 21, 2003
  2. Remy Cool
    Replies:
    1
    Views:
    449
    Remy Cool
    Aug 27, 2003
  3. Roman Neuhauser
    Replies:
    0
    Views:
    691
    Roman Neuhauser
    Apr 4, 2005
  4. Replies:
    2
    Views:
    691
    velle
    Jan 5, 2006
  5. John Salerno
    Replies:
    3
    Views:
    302
    John Salerno
    Mar 5, 2006
Loading...

Share This Page