如何导入其他Python文件?
要在代码中使用任何程序包,必须首先使其可访问。您必须导入它。在定义之前,您不能在Python中使用任何东西。有些东西是内置的,例如基本类型(如int,float等)都可以在需要时使用。但是,您要做的大多数事情都需要做更多。例如,如果要计算1弧度的余弦值,并且运行math.cos,则会得到NameError,因为未定义math。
示例
您需要告诉python首先在代码中导入该模块,以便您可以使用它。
>>> math.cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined >>> import math >>> math.cos(0) 1.0
如果您要导入自己的python文件,则可以使用import语句,如下所示:
>>> import my_file # assuming you have the file, my_file.py in the current directory.
# For files in other directories, provide path to that file, absolute or relative.热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短