我有一个我正在构建的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和任何导入它的脚本存在于同一个目录中。

其他回答

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

不幸的是,你不知道。这是导入语法工作的一部分,它这样做很重要——记住def实际上是执行的东西,如果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和任何导入它的脚本存在于同一个目录中。

由于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()

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

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