Python import总结
可能网上很多文章或博客都没解释清楚,作者自己也苦心于Python的import。至此,把自己的总结的分享给大家,本文不做基础讲解,仅说明疑惑的地方。
新版本的Pycharm 2017.1.1,对自己定义的模块都有提示,写代码的提示(如方法,变量)。
import的本质,针对包和模块来说,对于版本Python2和Python3来说,意义一样的,仅拿Python2做实验说明,Python3不做赘述。Python2版本Python2.7.13,Python3版本Python3.6.1.
导入模块的本质是把python文件解释和执行一遍。
test.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
print("I am test module.")
下面在同级目录TestImport.py中导入该模块:
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import test
执行命令:
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
I am test module.
导入包的本质是执行包下的__init__.py文件。
dir1/__init__.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
print("I am dir1package.")
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import dir1
执行命令:
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
I am dir1 package.
普通导入import test,模块下的全部对象全部导入,这里不做说明;下面是对模块进行对象的限制导入说明。
test.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
__all__=["A","printInfo"]
A = "Python"
B = "Java"
C = "Php"
def printInfo():
print("I am test module.")
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import test
# from test import *
print(test.A)
print(test.B)
print(test.C)
test.printInfo()
执行python:
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
Python
Java
Php
I am test module.
发现所有对象都导入了。
使用from x import *导入:
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
# import test
from test import *
print(A)
print(B)
print(C)
printInfo()
执行命令:
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
Python
Traceback (most recent call last):
File"C:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py",line 10, in <module>
print(B)
NameError: name 'B' is not defined
注释掉__all__中没有的对象,如下:
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
# import test
from test import *
print(A)
# print(B)
# print(C)
printInfo()
执行命令:
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
Python
I am test module.
总结:模块中__all__对对象的限制,仅对fromimport有效,对import无效。
在__init__.py文件中,直接import该包下的模块。
导入dir1包:
dir1/__init__.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
'''
1.这个文件在导入包时,确实会被执行;
'''
import test11
import test12
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import dir1
dir1.test11.printInfo()
dir1.test12.printInfo()
执行命令:
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
I am test11.
I am test12.
在__init__.py文件中,定义__all__导入:
导入dir1包:
dir1/__init__.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
'''
1.这个文件在导入包时,确实会被执行;
'''
__all__=["test11","test12"]
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
from dir1 import *
test11.printInfo()
test12.printInfo()
执行命令:
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
I am test11.
I am test12.
Python2中用from package import*可导入package下__init__.py文件限制的模块。将解释器切换至Python3,发现也可正常执行。
在__init__.py文件中,使用相对路径导入:
dir1/__init__.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
'''
1.这个文件在导入包时,确实会被执行;
'''
from . import test11
from . import test12
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import dir1
# from dir1 import *
dir1.test11.printInfo()
dir1.test12.printInfo()
或者
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
# import dir1
from dir1 import *
test11.printInfo()
test12.printInfo()
上述Python都能正常运行在Python2中,切换python解释器3,发现也能正常运行。
导入dir1包:
dir1/__init__.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
'''
1.这个文件在导入包时,确实会被执行;
'''
import test11
import test12
TestImport.py文件如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import os,sys
import dir1
dir1.test11.printInfo()
dir1.test12.printInfo()
执行命令:
C:\Python36\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
Traceback (most recent call last):
File"C:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py",line 6, in <module>
import dir1
File"C:\Users\Administrator\PycharmProjects\Foundation\FuckImp\dir1\__init__.py",line 8, in <module>
importtest11
ModuleNotFoundError: No module named 'test11'
总结:包中__init__.py文件中直接importmodule,调用该包时,Python2中能正常运行,Python3中却异常报错。
下面是dir1下的__init__文件
#!/usr/bin/env python # -*- coding:utf-8 -*- # author: Changhua Gong ''' 1.这个文件在导入包时,确实会被执行; ''' from . import test11 from . import test12
交互式环境
C:\Users\Administrator\PycharmProjects\Foundation>python 顶级目录
>>> from FuckImp import dir1
>>> dir1.test11.printInfo() # 加上包名就可调用了
I am test11.
在python项目中
TestImp.py如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
from FuckImp import dir1
dir1.test11.printInfo()# 加上包名就可调用了
运行
C:\Python27\python.exeC:/Users/Administrator/PycharmProjects/Foundation/FuckImp/TestImport.py
I am test11.
Python2和Python3一样有效