Strange problem when using imp.load_module

pythoncurious at gmail.com pythoncurious at gmail.com
Thu Apr 23 16:07:19 EDT 2009


Hi,

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
problem.
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:

.E
======================================================================
ERROR: test_2 (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_import.py", 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?

Thanks
/Mattias

Here's the code:

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

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

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

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

class Test(unittest.TestCase):
    def tearDown(self):
        os.unlink(module_file_name)

    def test_1(self):
        module_data='''x=1'''
        write_module(module_data)
        imported=import_from_file(module_file_name)
        getattr(imported, 'x')

    def test_2(self):
        # time.sleep(1)
        module_data='''y=2'''
        write_module(module_data)
        imported=import_from_file(module_file_name)
        getattr(imported, 'y')

if __name__ == "__main__":
    unittest.main()



More information about the Python-list mailing list