Python imp - Access the import internals - imp.load_source{name, pathname[, file]}
imp - Access the import internals
https://docs.python.org/2.7/library/imp.html
This module provides an interface to the mechanisms used to implement the import
statement.
此模块提供了用于实现 import
语句的机制的接口。
mechanism ['mek(ə)nɪz(ə)m]:n. 机制,原理,途径,进程,机械装置,技巧
internal [ɪn'tɜːn(ə)l]:n. 内脏,本质 adj. 内部的,里面的,体内的,内部的
1. imp.load_source(name, pathname[, file])
Load and initialize a module implemented as a Python source file and return its module object. If the module was already initialized, it will be initialized again. The name
argument is used to create or access a module object. The pathname
argument points to the source file. The file
argument is the source file, open for reading as text, from the beginning. It must currently be a real file object, not a user-defined class emulating a file. Note that if a properly matching byte-compiled file (with suffix .pyc
or .pyo
) exists, it will be used instead of parsing the given source file.
加载并初始化实现为 Python 源文件的模块并返回其模块对象。如果模块已经初始化,它将再次初始化。name
参数用于创建或访问模块对象。pathname
参数指向源文件。file
参数是源文件,从头开始打开以作为文本读取。它当前必须是真实的文件对象,而不是模拟文件的用户定义的类。请注意,如果存在正确匹配的字节编译文件 (带后缀 .pyc
或 .pyo
),则将使用它而不是解析给定的源文件。
suffix ['sʌfɪks]:n. 后缀,词尾 v. 添后缀
2. sys.path.append()
forever.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
def print_function(first_name, last_name):
print(first_name)
print(last_name)
yongqiang.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import sys
sys.path.append("/home/strong/git_workspace/MonoGRNet/strong/")
import forever
forever.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$
3. imp.load_source
forever.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
def print_function(first_name, last_name):
print(first_name)
print(last_name)
yongqiang.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import imp
imp.load_source("tian", "/home/strong/git_workspace/MonoGRNet/strong/forever.py")
import tian
tian.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$
4. imp.load_source
forever.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
def print_function(first_name, last_name):
print(first_name)
print(last_name)
yongqiang.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import imp
model = imp.load_source("tian", "/home/strong/git_workspace/MonoGRNet/strong/forever.py")
model.print_function("yongqiang", "cheng")
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py
yongqiang
cheng
strong@foreverstrong:~/git_workspace/MonoGRNet$
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/