Python导入包的注意事项

2025-12-01 0 92,396

本文教程操作环境: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基础教程

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 Python导入包的注意事项 https://www.zhiup.top/2202.html

相关