美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

创新互联Python教程:Python导入包的注意事项

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、使用注意

(1)只是导入包不能随便使用其中的模块,要导入到具体模块或者变量的层次

(2)文件夹与文件之间可以用.也可以用from import格式,而文件与里面的变量之间只能用from import格式,即不能import folder1.abcd.b

2、实例

>>> import folder1
>>> folder1.abcd.b
Traceback (most recent call last):  
File "", line 1, in 
AttributeError: module 'folder1' has no attribute 'abcd'
>>> from folder1 import abcd
>>> bob = abcd.Myclass(name = 'Bob', age = 20)
>>> bob.name
'Bob'
>>> bob.get_info()my name is Bob and age is 20
>>> from folder1.abcd import b
>>> b
2
>>> import folder1.abcd
>>> abcd.bTraceback (most recent call last):  File "", line 1, in NameError: name 'abcd' is not defined
>>> folder1.abcd.b2
>>> import folder1.abcd as aa
>>> aa.b
2

以上就是Python导入包的注意事项,希望能对大家有所帮助。更多Python学习指路:创新互联python教程


当前文章:创新互联Python教程:Python导入包的注意事项
本文网址:http://www.zsjierui.cn/article/djjjocs.html

其他资讯