回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在处理两个python文件。完成后,我计划从另一个打电话:</p>
<pre><code>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
</code></pre>
<p>如果我打电话主.py,我希望它也能运行<code>subfile.py</code>。偶尔,我会单独运行<code>subfile.py</code>,并希望它自己正常运行。现在,在<code>subfile.py</code>中,我应该将import命令嵌套在<code>if __name__ == "__main__"</code>下吗?你知道吗</p>
<pre><code>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
</code></pre>