August 10, 2019 Dynamically importing modules Do unto others do not impose on others

the module_t = __import__ ( ' study.test1 ' ) # Import module study, although there is introduced, but test1 top module, but once performed test1 
Print (the module_t) 
module_t2 = __import__ ( ' test1 ' ) # introduction of the current path test1 
Print (module_t2) 

() module_t.test1.test 
module_t2.test () 
# two paths Note that the above difference

>>>

<module 'study' from '/Users/miaowu/PycharmProjects/untitled/study/__init__.py'>
执行 test1
<module 'test1' from '/Users/miaowu/PycharmProjects/untitled/study/test1.py'>
test1
test1

from study.test1 Import * 
test1 () 
test2 () # if test2 front _ beginning, can not be imported by import *
from study.test1 Import test1, _test2 
test1 () 
_test2 () # _test2 () method
Import the importlib 
m = importlib.import_module ( ' study.test1 ' )
 Print (m) # perform test1, and test1 module is introduced, with the difference __import__ 
m.test1 () 
m._test2 ()

 

Guess you like

Origin www.cnblogs.com/python1988/p/11332819.html