【发布时间】:2018-03-13 15:48:06
【问题描述】:
我正在处理两个 python 文件。完成后,我打算从另一个人那里打电话:
main.py
import os, re, time, logging, sys, subprocess, operator, datetime, pprint, dbfread, collections, calendar, xlwt, xlrd, errno, platform, stat
import subfile
# A long body of codes that does things
subfile.py
import os, re, time, logging, sys, subprocess, operator, datetime, pprint, dbfread, collections, calendar, xlwt, xlrd, errno, platform, stat
# Another long body of codes that does things
如果我调用 main.py,我希望它也能运行 subfile.py。偶尔,我会单独运行subfile.py,并希望它自己正常运行。现在,在subfile.py 中,我应该将导入命令嵌套在if __name__ == "__main__" 下吗?
subfile.py
if __name__ == "__main__":
import os, re, time, logging, sys, subprocess, operator, datetime, pprint, dbfread, collections, calendar, xlwt, xlrd, errno, platform, stat
# Another long body of codes that does things
【问题讨论】: