您好,欢迎来到花图问答。
搜索
您的当前位置:首页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 "<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学习指路:

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文

Copyright © 2019-2025 huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务