forked from whai362/PSENet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.py
57 lines (48 loc) · 1.37 KB
/
mod.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#coding=utf-8
import logging
def add_to_path(path):
'''
add path to sys.path.
'''
import sys;
sys.path.insert(0, path);
def add_ancester_dir_to_path(fp, p):
'''
add ancester directory to sys.path.
fp: usually __file__
p : the relative path to be added.
'''
import util
parent_path = util.io.get_dir(fp)
path = util.io.join_path(parent_path, p)
add_to_path(path)
def is_main(mod_name):
return mod_name == '__main__'
def import_by_name(mod_name):
__import__(mod_name)
return get_mod_by_name(mod_name)
def try_import_by_name(mod_name, error_path):
try:
import_by_name(mod_name)
except ImportError:
logging.info('adding %s to sys.path'%(error_path))
add_to_path(error_path)
import_by_name(mod_name)
return get_mod_by_name(mod_name)
def get_mod_by_name(mod_name):
import sys
return sys.modules[mod_name]
def load_mod_from_path(path, keep_name = True):
""""
Params:
path
keep_name: if True, the filename will be used as module name.
"""
import util
import imp
path = util.io.get_absolute_path(path)
file_name = util.io.get_filename(path)
module_name = file_name.split('.')[0]
if not keep_name:
module_name = '%s_%d'%(module_name, util.get_count())
return imp.load_source(module_name, path)