Import module with non-standard file name

Patrick Maupin pmaupin at gmail.com
Tue Aug 8 01:01:13 EDT 2006


Ben Finney wrote:
> Howdy all,
>
> 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.

Regards,
Pat




More information about the Python-list mailing list