python 模块导入学习
相同目录下的import
module_test --- a.py --- b.py
#a.pydef a_test(): print "a_test"
#b.pydef b_test(): print "b_test"
b 中导入 a 的方法
import adef b_test(): print ('b_test')a.a_test()------------------------------a_test
通过import 语句导入成功,因为他们在同一级别上,也可以改成from, import的形式
from a import a_testdef b_test(): print ('b_test')a_test()-----------------------------a_test
不同目录下的import
在module_test下面建立两个包文件,subpackage_1, subpackage_2
module_test --- a.py --- b.py --- subpackage_1 --- __init__.py --- sub_package_1.py --- subpackage_2 --- __init__.py --- sub_package_2.py
#sub_package_1.pydef sub_package_1_test(): print "sub_package_1_test"
#sub_package_2.pydef sub_package_2_test(): print "sub_package_2_test"
如果sub_package_2要调用sub_package_1中的方法 错误方法
from subpackage_1.sub_package_1 import sub_package_1_testdef sub_package_2_test(): print "sub_package_2_test"sub_package_1_test()------------------------------I:\Python\tutorial\module_test\subpackage_2>python sub_package_2.pyTraceback (most recent call last): File "sub_package_2.py", line 1, infrom subpackage_1.sub_package_1 import sub_package_1_testImportError: No module named subpackage_1.sub_package_1
因为不在同一目录,python找不到
- 绝对路径
import syssys.path.append("I:\\Python\\tutorial\\module_test")from subpackage_1.sub_package_1 import sub_package_1_testdef sub_package_2_test(): print "sub_package_2_test"sub_package_1_test()------------------------------sub_package_1_test
- 错误相对路径
from ..subpackage_1.sub_package_1 import sub_package_1_testdef sub_package_2_test(): print "sub_package_2_test"sub_package_1_test()------------------------------I:\Python\tutorial\module_test\subpackage_2>python sub_package_2.pyTraceback (most recent call last): File "sub_package_2.py", line 2, infrom ..subpackage_1.sub_package_1 import sub_package_1_testValueError: Attempted relative import in non-package
找不到上一级的包是因为执行的sub_package_2为顶层,没有上一级了
另一种错误形式
module_test/ --- sub_package_1/ --- __init__.py --- sub_package_1.py --- sub_package_2 --- __init__.py --- sub_package_2.py --- __init__.py --- a.py --- b.py
此时 sub_package_2.py 能相对调用sub_package_1的模块吗
#sub_package_2.pyfrom ..subpackage_1.sub_package_1 import sub_package_1_testdef sub_package_2_test(): print "sub_package_2_test" sub_package_1_test()
..指的是上一级目录,上一级目录中的sub_package_1中的模块,再由b.py来调用,由b这一层来做顶层
#b.pyfrom a import a_testfrom package.subpackage_2.sub_package_2 import sub_package_2_testdef b_test(): print ('b_test')sub_package_2_test()a_test()----------------------------I:\Python\tutorial\module_test>python b.pyTraceback (most recent call last): File "b.py", line 2, infrom subpackage_2.sub_package_2 import sub_package_2_test File "I:\Python\tutorial\module_test\subpackage_2\sub_package_2.py", line 2, in from ..subpackage_1.sub_package_1 import sub_package_1_testValueError: Attempted relative import beyond toplevel package
因为执行b.py时已经不能把module_test看成是一个完整的包,即使它有__init__.py,既然不能看做是包,那sub_package_2.py中的..上一层就不是包,因此顶层就是subpackage_2/这一层,..就超过顶层了
正确的相对导入
module_test/ --- package/ --- subpackage_1/ ---__init__.py --- sub_package_1.py --- subpackage_2/ --- __init__.py --- sub_package_2.py --- __init__.py --- a.py --- b.py
相对导入指的是包内的相对导入,如果是顶层文件则不存在相对导入,顶层文件指的是main()指行文件,因此执行文件只能是a.py或者是b.py, package作为一个包,sub_packge作为子包,形成包内的路径
#sub_package_2.pyfrom ..subpackage_1.sub_package_1 import sub_package_1_testdef sub_package_2_test(): print "sub_package_2_test" sub_package_1_test()
sub_package2相对导入subpackage_1中的文件,是因为上层也是一个完整的包
from a import a_testfrom package.subpackage_2.sub_package_2 import sub_package_2_testdef b_test(): print ('b_test')sub_package_2_test()a_test()-----------------------I:\Python\tutorial\module_test>python b.pysub_package_2_testsub_package_1_testa_test
导入 a 是同级导入, 导入子模块只需要.运算符,但是却省掉了绝对路径的sys.append,代码放在哪也不重要了,因为是相对路径,也不是硬编码,灵活多变
总结1. 同级目录可以import2. 不同级目录,如果需要执行module里面的文件的,则需要绝对路径3. 绝对跟径导入代码清析,但是硬编码,对文件存放放路径有特殊要求4. 相对路径需要清楚相对的是包内调用,如果不是包就不能谈,且顶层文件需要在包外,否则与顶层文件同一层的不能为包
参考
http://kuanghy.github.io/2016/07/21/python-import-relative-and-absolute 相对导入与约对导入是针对包内部https://laike9m.com/blog/pythonxiang-dui-dao-ru-ji-zhi-xiang-jie,60/ 相对导入机制http://blog.csdn.net/liberize/article/details/8882659 三个例题解释了相对导入http://codingpy.com/article/python-import-101/ 几种导入的方式http://hiaero.net/python-relative-imports/ 简明的说明相对导入