1.import_module做用python
能夠經過字符串來導入模塊,同一文件夾下字符串爲模塊名,不一樣文件夾字符串爲模塊的路徑python2.7
2.用法及示例spa
建立foo.pycode
1 # !/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 5 def main(): 6 print __file__
建立bar.pyblog
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import importlib 4 5 a = importlib.import_module('foo') 6 b = a.main()
運行bar.py,結果以下:utf-8
1 /usr/bin/python2.7 /home/Desktop/testpy/TTtest/bar.py 2 /home/Desktop/testpy/TTtest/foo.py 3 4 Process finished with exit code 0