我有一个我正在构建的Python程序,可以以两种方式之一运行:第一种是调用Python main.py,它会以友好的方式提示用户输入,然后通过程序运行用户输入。另一种方法是调用python batch.py -file-它将传递所有友好的输入收集,并通过程序一次性运行整个文件的输入值。

问题是,当我运行batch.py时,它从main.py中导入了一些变量/方法/等,当它运行以下代码时:

import main

在程序的第一行,它立即出错,因为它试图运行main.py中的代码。

如何阻止Python运行我正在导入的主模块中包含的代码?


当前回答

使用if __name__ == '__main__'习惯用法——__name__是一个特殊变量,如果模块作为脚本运行,其值为'__main__',如果模块被导入,则为模块名。所以你会这样做

# imports
# class/function definitions
if __name__ == '__main__':
    # code here will only run when you invoke 'python main.py'

其他回答

因为这就是Python的工作原理——class和def等关键字不是声明。相反,它们是实际执行的实时语句。如果它们没有被执行,您的模块将是空的。

惯用的方法是:

# stuff to run always here such as class/def
def main():
    pass

if __name__ == "__main__":
   # stuff only to run when not called via 'import' here
   main()

但是,它确实需要对导入的模块进行源代码控制。

A minor error that could happen (at least it happened to me), especially when distributing python scripts/functions that carry out a complete analysis, was to call the function directly at the end of the function .py file. The only things a user needed to modify were the input files and parameters. Doing so when you import you'll get the function running immediately. For proper behavior, you simply need to remove the inside call to the function and reserve it for the real calling file/function/portion of code

使用if __name__ == '__main__'习惯用法——__name__是一个特殊变量,如果模块作为脚本运行,其值为'__main__',如果模块被导入,则为模块名。所以你会这样做

# imports
# class/function definitions
if __name__ == '__main__':
    # code here will only run when you invoke 'python main.py'

由于Python的工作方式,当它导入模块时,有必要运行它们。

为了防止模块中的代码在导入时被执行,但只在直接运行时执行,你可以使用以下if来保护它:

if __name__ == "__main__":
    # this won't be run when imported

您可能希望将此代码放在main()方法中,以便可以直接执行文件,或者导入模块并调用main()。例如,假设这是在文件foo.py中。

def main():
    print "Hello World"

if __name__ == "__main__":
    main()

这个程序可以通过python foo.py运行,也可以通过另一个python脚本运行:

import foo

...

foo.main()

我做了一个简单的测试:

# 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