2013-06-30 4 views
0

python test.py ab_mr1으로 스크립트를 실행 중이며 branch_name의 결과는 "ab_mr1"이어야합니다.하지만 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') 

답변

3

혼란스러워합니다. test.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 
+0

당신이있는 바로, 어리석은 실수 ... 감사합니다 – user2341103

관련 문제