python 28 动态导入模块

# 导入 m1/t.py module_t 实际上是指向到m1
module_t = __import__("m1.t")
print(module_t)  # <module 'm1' from '/home/a5673/xuzj/05_code/python_base/03_python_base/day26/m1/__init__.py'>
module_t.t.test1()  # m1 -> t.py : test1()

# 通过 import * 模块中带下划线的方法将不被导入
from m1.t import *
test1()  # m1 -> t.py : test1()
test2()  # m1 -> t.py : test2()
_test3()  # NameError: name '_test3' is not defined

from m1.t import test1, test2, _test3
test1()  # m1 -> t.py : test1()
test2()  # m1 -> t.py : test2()
_test3()  # m1 -> t.py : _test3()

# 通过 importlib 导入
import importlib
m = importlib.import_module("m1.t")
print(m)  # <module 'm1.t' from '/home/a5673/xuzj/05_code/python_base/03_python_base/day26/m1/t.py'>
m.test1()  # m1 -> t.py : test1()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值