pythonimport是什么
本文讲解一下python中的import语句。刚刚接触这门语言的朋友可能并不懂Pythonimport语句是什么意思。那么这篇文章就来带大家来认识一下import语句,了解一下python中import的用法。
Python语言中import的使用很简单,直接使用importmodule_name语句导入即可。这里我主要写一下"import"的本质。
Python官方定义:Pythoncodeinonemodulegainsaccesstothecodeinanothermodulebytheprocessofimportingit.
定义
模块(module):用来从逻辑(实现一个功能)上组织Python代码(变量、函数、类),本质就是*.py文件。文件是物理上组织方式"module_name.py",模块是逻辑上组织方式"module_name"。
包(package):定义了一个由模块和子包组成的Python应用程序执行环境,本质就是一个有层次的文件目录结构(必须带有一个__init__.py文件)。
import语句:
在模块模块定义好后,我们可以使用import语句来引入模块,语法如下:
importmodule1[,module2[,...moduleN]
form...import语句:
Python的from语句让你从模块中导入一个指定的部分到当前命名空间中。语法如下:
frommodnameimportname1[,name2[,...nameN]]
例如,要导入模块fib的fibonacci函数,使用如下语句:
fromfibimportfibonacci
(这个声明不会把整个fib模块导入到当前的命名空间中,它只会将fib里的fibonacci单个引入到执行这个声明的模块的全局符号表。)
form...import*语句:
把一个模块的所有内容全都导入到当前的命名空间也是可行的,只需使用如下声明:
frommodnameimport*
这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。
例如我们想一次性引入math模块中所有的东西,语句如下:
frommathimport*
以上就是今天所要说明的内容,import语句的定义以及用法,光是看解释与我所举的例子只是纸上谈兵,动手实践才是验证所学的最好的方法与途径。
以上内容为大家介绍了python培训之import是什么,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






