I am trying to call Branchname defined in test.py from test1.py and running into following error,can anyone provide inputs?
test.py
import test1
import os
import sys
def main():
#initialize global variables & read CMD line arguments
global BranchName
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"
test1.main()
print "After running test1"
if __name__ == '__main__':
main()
test1.py
import test
def main ():
print test.BranchName
Running into following error
BranchName
ab_mr2
Before running test1
Traceback (most recent call last):
File "test.py", line 18, in <module>
main()
File "test.py", line 14, in main
test1.main()
File "/local/mnt/workspace/test1.py", line 3, in main
print test.BranchName
AttributeError: 'module' object has no attribute 'BranchName
-
You have a circular import here, which is never good. Is there a reason the code is split up like this?Burhan Khalid– Burhan Khalid2013年06月30日 05:20:23 +00:00Commented Jun 30, 2013 at 5:20
-
@BurhanKhalid - my goal is to print BranchName passed to test.py from test1.py..user2341103– user23411032013年06月30日 05:24:55 +00:00Commented Jun 30, 2013 at 5:24
-
1You arent passing anything here.Burhan Khalid– Burhan Khalid2013年06月30日 05:26:20 +00:00Commented Jun 30, 2013 at 5:26
-
You have a test.py that is the main script you call. Then you don't run test.py, but run test1.py instead. but still assume that test.py is the main function. That doesn't really make any sense, and I think you should think about how you split things up.Lennart Regebro– Lennart Regebro2013年06月30日 05:32:20 +00:00Commented Jun 30, 2013 at 5:32
2 Answers 2
main() does not actually get called in your test.py, because __name__ != '__main__'.
If you print __name__, it is actually test.
This is a reason why many scripts have the if __name__ == '__main__', so if it is imported, the whole code isn't run.
To fix this, you have to do two things:
You can just remove the
if __name__ == '__main__':in yourtest.py, and just replace it withmain()There is no need to
import test1.pyin your test. In doing so, this is actually runningmain()in yourtest1.py, and will thus raise an error becausetest.BranchNamehasn't even been defined yet.- However, if you must import
test1.py, you can actually put anif __name__ == '__main__'in there, so when you import it fromtest.py, it will not run.
- However, if you must import
6 Comments
__name__ != '__main__'my goal is to print BranchName passed to test.py from test1.py
If this is your case, then your file names are reversed. Also, you aren't passing anything around (which you should, instead of playing with global).
In test1.py, calculate BranchName, and then pass it to the main method from test.
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()
In test.py, you have simply:
def main(branch_name):
print('In test.py, the value is: {0}', branch_name)
2 Comments
ScriptDir is the starting point, which is why its in test1.py. If you put this line in test.py, you'll get an error because you won't have anything to pass to main (ScriptDir won't be set).