本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
1、使用注意
(1)只是导入包不能随便使用其中的模块,要导入到具体模块或者变量的层次
(2)文件夹与文件之间可以用.也可以用from import格式,而文件与里面的变量之间只能用from import格式,即不能import folder1.abcd.b
2、实例
>>>importfolder1 >>>folder1.abcd.b Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> AttributeError:module'folder1'hasnoattribute'abcd' >>>fromfolder1importabcd >>>bob=abcd.Myclass(name='Bob',age=20) >>>bob.name 'Bob' >>>bob.get_info()mynameisBobandageis20 >>>fromfolder1.abcdimportb >>>b 2 >>>importfolder1.abcd >>>abcd.bTraceback(mostrecentcalllast):File"<stdin>",line1,in<module>NameError:name'abcd'isnotdefined >>>folder1.abcd.b2 >>>importfolder1.abcdasaa >>>aa.b 2
以上就是Python导入包的注意事项,希望能对大家有所帮助。更多Python学习指路:python基础教程