导入时是否执行.py [python3]

时间:2014-09-21 13:26:14

标签: python sql python-3.x sqlalchemy python-import

我需要执行一个文件,所以我想知道它是否像这样工作?需要执行的文件将创建一个数据库。

1 个答案:

答案 0 :(得分:1)

好的,我也在这里勾勒出答案。如果代码未封装在类或函数中,则代码将运行。例如:

def f():
    print ('This line will not be executed during import from another file')

print ('This line is executed!')

if __name__ == '__main__':
    print ('This line is executed only, if the program is main program, but not during import from another program.')