31 lines
583 B
Cython
31 lines
583 B
Cython
import imp
|
|
|
|
def import_module(path):
|
|
"""
|
|
Imports a python file as a module. The path can be relative or absolute.
|
|
|
|
Based on the work of Yuval Greenfield released into the public domain.
|
|
"""
|
|
|
|
# remove the .py suffix
|
|
mod_dn = os.path.dirname(path)
|
|
mod_fn = os.path.basename(path)
|
|
|
|
if mod_fn.endswith(".py"):
|
|
mod_name = mod_fn[:-3]
|
|
else:
|
|
# packages for example
|
|
mod_name = mod_fn
|
|
|
|
fd = None
|
|
|
|
try:
|
|
data = imp.find_module(mod_name, [mod_dn])
|
|
module = imp.load_module(mod_name, *data)
|
|
fd = data[0]
|
|
|
|
finally:
|
|
if fd is not None:
|
|
fd.close()
|
|
|
|
return module
|