importing with .m instead of .py

wisccal wisccal at googlemail.com
Sat Sep 26 11:55:05 EDT 2009


Wanderer wrote:
> Is there a way to get python to import files that don't end in .py?

How about using imp.load_source?

y at x:/tmp$ cat out.m
print("this is my module")
# comment
y=[1,2,3,4,5,6,7,8,9];
# comment
y at x:/tmp$ cat load_my_module.py
import imp
MyModule=imp.load_source('MyModule','out.m')
print("y is {0}".format(MyModule.y))
y at x:/tmp$ p31 load_my_module.py
this is my module
y is [1, 2, 3, 4, 5, 6, 7, 8, 9]

Regards
wisccal
http://skre.ch




More information about the Python-list mailing list