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

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

import main

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

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


当前回答

试着从main.py导入所需的函数?所以,

from main import SomeFunction

可能是你在batch.py中命名的函数与main.py中的函数相同,当你导入main.py时,程序运行main.py函数而不是batch.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()

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

将代码放入函数中,直到调用该函数它才会运行。在main.py中应该有一个main函数。声明如下:

if __name__ == '__main__':
  main()

然后,如果调用python main.py, main()函数将运行。如果你导入main.py,它就不会。此外,为了清晰起见,您可能应该将main.py重命名为其他名称。

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

你可以这样写"main.py":

#!/usr/bin/env python

__all__=["somevar", "do_something"]

somevar=""

def do_something():
    pass #blahblah

if __name__=="__main__":
    do_something()

虽然你不能在不运行代码的情况下使用import;有一种非常快速的方法可以输入你的变量;通过使用numpy。Savez,它将变量作为numpy数组存储在.npz文件中。然后,您可以使用numpy.load加载变量。

在scipy文档中可以看到完整的描述

请注意,这只适用于变量和变量数组,而不适用于方法等。