Strange problem when using imp.load_module

pythoncurious at pythoncurious at
Thu Apr 23 22:07:19 CEST 2009


I'm having problem when I'm trying to import modules using the
imp.load_module function.
At the end of this post there's some code I wrote to illustrate the
The code istself doesn't make much sense, but what I'm trying to do in
reality is allow people to customize an application by writing a small
python module. If I find a file in a known location with a known name,
I'll import it and use some data or function in it.
I ran into problems when writing unit tests for it.

What happens when run my code is this:

ERROR: test_2 (__main__.Test)
Traceback (most recent call last):
  File "", line 30, in test_2
    getattr(imported, 'y')
AttributeError: 'module' object has no attribute 'y'
Ran 2 tests in 0.015s

So it seems that test_2 fails. It does pretty much the same thing as
test_1, but with different data.
Changing test_2 to check for 'x' instead of 'y' makes it pass, but
that's not the result I should get.
So it seems that I still have the module that I imported in test_1.

So, I'm thinking that I might be dealing with some sort of gc issue.
I add the "time.sleep(1)", (commented in the code) and the tests pass.
'y' is found in test_2.
Replacing the sleep with a gc.collect() makes the test fail again, so
garbage collection doesn't seem to help.

I've tried it on python 2.6.1 on windows, 2.5.2 in cygwin and 2.6.1 on
solaris with the same results.
Could anyone explain what I am missing?


Here's the code:

import unittest
import tempfile, os, imp, time, gc

module_file_name=os.path.join(tempfile.gettempdir(), '')

def write_module(data):
    f = open(module_file_name, 'w')

def import_from_file(path):
    imported_module = imp.load_source(module_file_name, path)
    return imported_module

class Test(unittest.TestCase):
    def tearDown(self):

    def test_1(self):
        getattr(imported, 'x')

    def test_2(self):
        # time.sleep(1)
        getattr(imported, 'y')

if __name__ == "__main__":

More information about the Python-list mailing list