Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止导入的python脚本运行,但仅在代码中调用时运行_Python_Python 2.7_Import_Python Import_Python Module - Fatal编程技术网

如何停止导入的python脚本运行,但仅在代码中调用时运行

如何停止导入的python脚本运行,但仅在代码中调用时运行,python,python-2.7,import,python-import,python-module,Python,Python 2.7,Import,Python Import,Python Module,当我导入一个python脚本并运行当前脚本时,它似乎正在运行并显示导入脚本的输出,这是一种非常不寻常的行为。我刚刚在脚本中导入了它,但在主代码中没有真正调用它的任何函数。我如何避免这种行为发生 如果我在主脚本中传递-d标志,它将只运行主脚本中的常规代码 如果我在主脚本中传递-t标志,它将只运行导入的python脚本中的代码 main.py import os import argparse import functions as funcs import generate_json as gen

当我导入一个python脚本并运行当前脚本时,它似乎正在运行并显示导入脚本的输出,这是一种非常不寻常的行为。我刚刚在脚本中导入了它,但在主代码中没有真正调用它的任何函数。我如何避免这种行为发生

如果我在主脚本中传递-d标志,它将只运行主脚本中的常规代码

如果我在主脚本中传递-t标志,它将只运行导入的python脚本中的代码

main.py

import os
import argparse
import functions as funcs
import generate_json as gen_json
from test_compare_filesets import tester as imptd_tester


def get_json_location():
    path = os.getcwd() + '/Testdata'
    return path


def main():

 parser = argparse.ArgumentParser()
 parser.add_argument("-d", "--export-date", action="store_true", required=True)
 parser.add_argument("-t", "--execute-test", action="store_true", required=False)
 args = parser.parse_args()
 date = args.export_date
 testt = args.execute_test

 yml_directory = os.listdir('yaml/')
 yml_directory.remove('export_config.yaml')



 with open('dates/' + date + '.json', 'w') as start:
  start.close()

 for yml in yml_directory :
   print("Running export for " + yml)
   yml_file = os.path.join('yaml/' + yml)

   json_path = get_json_location()
   yml = funcs.read_config(yml_file)
   data_folder = date
   gen_json.generate_data_report(json_path , yml , data_folder)


if __name__ == '__main__':


   main()
test_files.py

import generate_report as generate_reportt


def compare_filesets(file_names, previous_data, current_data):
    for item in file_names:
        print(item + generate_reportt.compare(previous_data.get(item), current_data.get(item)) + "\n")


def test_filesets():

 '''
Test for scenario 1
'''

dict_1 = generate_reportt.read_file_into_dict("dates/2018-01-01.json")
dict_2 = generate_reportt.read_file_into_dict("dates/2018-01-02.json")
print(" Test 1 ")
compare_filesets(file_names=['a.json', 'b.json', 'c.json'],
                 previous_data=dict_1,
                 current_data=dict_2
                 )

这就是为什么使用该语句:

if __name__ == "__main__":
    main()
这是非常重要的。您需要将其添加到正在导入的脚本中,并将所有正在调用的代码放入该脚本中的
main()
函数中。脚本的变量
\uuuu name\uuuu
会根据是否导入脚本而变化。如果您没有导入脚本并运行它,那么该脚本的
\uuuuu name\uuuuu
变量将是
“\uuuuuu main\uuuu”
。但是,如果导入,则
\uuu name\uuu
变量将变成脚本的文件名,因此该脚本的
main()
函数中的所有内容都不会运行


有关更多信息:

如果这对您有效,请将此标记为答案:)