我有一个我正在构建的Python程序,可以以两种方式之一运行:第一种是调用Python main.py,它会以友好的方式提示用户输入,然后通过程序运行用户输入。另一种方法是调用python batch.py -file-它将传递所有友好的输入收集,并通过程序一次性运行整个文件的输入值。
问题是,当我运行batch.py时,它从main.py中导入了一些变量/方法/等,当它运行以下代码时:
import main
在程序的第一行,它立即出错,因为它试图运行main.py中的代码。
如何阻止Python运行我正在导入的主模块中包含的代码?
Another option is to use a binary environment variable, e.g. lets call it 'run_code'. If run_code = 0 (False) structure main.py to bypass the code (but the temporarily bypassed function will still be imported as a module). Later when you are ready to use the imported function (now a module) set the environment variable run_code = 1 (True). Use the os.environ command to set and retrieve the binary variable, but be sure to convert it to an integer when retrieving (or restructure the if statement to read a string value),
在main.py:
import os
#set environment variable to 0 (False):
os.environ['run_code'] = '0'
def binary_module():
#retrieve environment variable, convert to integer
run_code_val = int(os.environ['run_code'] )
if run_code_val == 0:
print('nope. not doing it.')
if run_code_val == 1:
print('executing code...')
# [do something]
...在任何加载main.py的脚本中:
import os,main
main.binary_module()
输出:不。不做。
# now flip the on switch!
os.environ['run_code'] = '1'
main.binary_module()
输出:执行代码…
*注意:上面的代码假设main.py和任何导入它的脚本存在于同一个目录中。
我做了一个简单的测试:
# test.py
x = 1
print("1, has it been executed?")
def t1():
print("hello")
print("2, has it been executed?")
def t2():
print("world")
print("3, has it been executed?")
def main():
print("Hello World")
print("4, has it been executed?")
print("5, has it been executed?")
print(x)
# while True:
# t2()
if x == 1:
print("6, has it been executed?")
# test2.py
import test
当执行或运行test2.py时,运行结果:
1, has it been executed?
5, has it been executed?
1
6, has it been executed?
结论:当导入的模块没有添加if __name__=="__main__":时,当前模块将运行,导入模块中不在函数中的代码将按顺序执行,而函数中未调用的代码将不执行。
此外:
def main():
# Put all your code you need to execute directly when this script run directly.
pass
if __name__ == '__main__':
main()
else:
# Put functions you need to be executed only whenever imported