Python模块的定义,模块的导入,__name__用法实例分析
2020-06-25 08:08:27 来源:易采站长站 作者:易采站长站整理
本文实例讲述了Python模块的定义,模块的导入,__name__用法。分享给大家供大家参考,具体如下:
相关内容:
什么是模块
模块的导入
模块的导入
自模块的导入
同级目录导入
不同级目录导入
目录内导入目录外
目录外导入目录内
__name__
什么是模块:
在Python中,模块就是一个个方法和类的仓库,如果我们想要使用某个模块中的某个方法或类,那么我们就需要导入对应的模板。
【python有内置方法、类,所以有些方法我们并不需要导入模块】
模块的使用:模块.函数 , 模块.类
#函数
import math
print(math.sqrt(3))
#类
import threading
a=threading.Thread()
print(a)
模块的导入:
模块导入:
import 模块名
#import 模块名
import math
print(math.sqrt(9))from…import 语句:从模块中导入一个指定的部分,如类、方法,其中from…import * 代表导入全部内容
#from 模块名 import 类名、方法名
from collections import Iterable
print(isinstance("abc",Iterable))
print(isinstance([1,2,3],Iterable))from…import导入的时候还能给导入的部分起个自定义的名字:如:from … import funcA as fa
自模块导入:
Python import 的搜索路径
1.在当前目录下搜索该模块
2.在环境变量 PYTHONPATH 中指定的路径列表中依次搜索 【可以从sys.path中获取】
import sys
print(sys.path)----------
运行结果:
['J:HardWorkCodepython',
'J:HardWorkCodepython',
'I:python3python36.zip',
'I:python3DLLs',
'I:python3lib',
'I:python3',
'I:python3libsite-packages']
3.在 Python 安装路径的 lib 库中搜索
同级目录导入:直接导入

![image_thumb[2] image_thumb[2]](https://www.easck.com/d/file/200625/20200625073300541.jpg)
![image_thumb[4] image_thumb[4]](https://www.easck.com/d/file/200625/20200625073301542.jpg)
不同级目录导入:将对应目录加入到sys.path中,再import













闽公网安备 35020302000061号