"Patrick Maupin" <[EMAIL PROTECTED]> writes: > Ben Finney wrote: > > Question: I have Python modules named without '.py' as the extension, > > and I'd like to be able to import them. How can I do that? > > This is a piece of cake in Python. > > >>> from types import ModuleType > >>> x = ModuleType('myModName') > >>> data = open('myfilename').read() > >>> exec data in x.__dict__ > Your output here... > > This won't save a .pyc, but as your message later explains, this is for > unittesting, so this could probably be considered a feature for this > usage.
Very nice. Okay, my unit testing scaffold module now has a new function: def make_module_from_file(module_name, file_name): """ Make a new module object from the code in specified file """ from types import ModuleType module = ModuleType(module_name) module_file = open(file_name, 'r') exec module_file in module.__dict__ return module The unit test now just imports that functionality, and then makes the module object via that function: import scaffold module_name = 'frobnicate_foo' module_file_under_test = os.path.join(scaffold.code_dir, 'frobnicate-foo') frobnicate_foo = scaffold.make_module_from_file( module_name, module_file_under_test) The rest of the unit test then has 'frobnicate_foo' as a module to test. It's working fine. Does anyone foresee any problems with doing it this way? -- \ "Injustice is relatively easy to bear; what stings is justice." | `\ -- Henry L. Mencken | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list