import导入模块
- import 搜索路径
程序执行时添加新的模块路径
- sys.path.append(‘/home/xxx’)
- sys.path.insert(0,’home/xxxx’)
动态导入模块
- xx =
__import__
(“xx”) # xx模块名
xx.func()
重新导入模块
- 模块一旦被导入就会保存到内存中,再次import同一个名字修改过的模块是没有用的,必须重新导入需用reload
模块test:
def test1():
print("---1---")
程序main:
import test
test.test1()
修改模块test:
def test1():
print("---2----")
再次在刚刚的程序main中:
import test
from imp import reload
test.test1()
import test
test.test1()
reload(test)
test.test1()
注意点
- import xx 可以理解成浅拷贝
- from xx import xx 可以理解成深拷贝,会在内存上完全复制一份
- 如果多个文件需要修改同一个文件的,使用(import xx)