0

としてスクリプトを実行していますpython test.py ab_mr1。出力は である必要があります"ab_mr1"branch_name、空の値として出力されています。

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 に答える