0

我正在运行脚本python test.py ab_mr1,输出应该是"ab_mr1"for branch_name,但它打印为 emtpy 值,知道为什么吗?

test1.py

import os
import sys

import test

def main():
    ScriptDir = os.getcwd()
    print ScriptDir
    BranchName  = sys.argv[1]
    print "BranchName"
    print BranchName
    #Update input file with external gerrits, if any
    print "Before running test1"
    test.main(BranchName) # here I am passing the variable
    print "After running test1"

if __name__ == '__main__':
    main()

test.py

branch_name=''
def main(branch_name):
    print('In test.py, the value is: {0}', branch_name)
if __name__ == '__main__': # need this
    main(branch_name)

电流输出:

('In test.py, the value is: {0}', '')

预期输出:

('In test.py, the value is: {0}', 'ab_mr1')
4

1 回答 1

3

你糊涂了。你在跑test.py不是 test1.py

运行test1.py让它调用test.main()。因为您正在运行test.py,所以它的__main__块正在运行并且branch_name是一个空字符串。

您的代码可以正常工作:

$ python test1.py ab_mr1
/private/tmp
BranchName
ab_mr1
Before running test1
('In test.py, the value is: {0}', 'ab_mr1')
After running test1
于 2013-06-30T17:14:43.443 回答