导语:
本文主要介绍了关于Python导入包的注意事项的相关知识,希望可以帮到处于编程学习途中的小伙伴
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
1、使用注意
(1)只有导入包不能随便使用里面的模块,必须导入到具体模块或变量的层次
(2) 可以使用文件夹和文件。也可以使用from import格式,但是里面的文件和变量之间只能使用from import格式,即不能导入folder1.abcd.b
2、实例
>>> import folder1
>>> folder1.abcd.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>NameError: name 'abcd' is not defined
>>> folder1.abcd.b2
>>> import folder1.abcd as aa
>>> aa.b
2
以上就是Python导入包
的注意事项,希望能对大家有所帮助。
更多Python学习指路:
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 什么是 python 归并排序12/06
- ♥ python列表缓存的探索09/27
- ♥ Python数据可视化库有哪些10/23
- ♥ 什么是 python pip10/24
- ♥ Python中Counter的两种用法10/10
- ♥ python如何输入平方根11/02
内容反馈