给定完整路径,如何导入Python模块?

给定完整路径,导入Python模块的最简单方法是将路径添加到path变量。path变量包含Python解释程序查找的目录,以查找在源文件中导入的模块。

例如

import sys
sys.path.append('/foo/bar/my_module')
# Considering your module contains a function called my_func, you could import it:
from my_module import my_func
# Or you could import the module as a whole,
import my_module