-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportFile.py
33 lines (31 loc) · 1004 Bytes
/
importFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# allows you to import a python module by giving the file path
# ie importFile( "/foo/bar/myModule.py" )
# instead of sys.path.append( "/foo/bar" ) and import( "myModule" )
#
def importFile(path):
from os.path import basename, isdir, splitext
import imp
import base64
if isdir(path):
realPath = join(path, "__init__.py")
coreName = base64.encodestring(realPath)
try:
mod = imp.load_source(coreName, realPath)
except IOError:
raise IOError("Failed to load directory module: %s\n" % path)
else:
baseName = basename(path)
(coreName, ext) = splitext(baseName)
coreName = base64.encodestring(path)
if ext == ".so":
try:
return imp.load_dynamic(coreName, path)
except IOError:
raise IOError("Failed to load dynamic module: %s\n" % path)
else:
try:
mod = imp.load_source(coreName, path)
except IOError:
raise IOError("Failed to load source module: %s\n" % path)
return mod