1

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
asked Jun 30, 2013 at 5:10
4
  • You have a circular import here, which is never good. Is there a reason the code is split up like this? Commented Jun 30, 2013 at 5:20
  • @BurhanKhalid - my goal is to print BranchName passed to test.py from test1.py.. Commented Jun 30, 2013 at 5:24
  • 1
    You arent passing anything here. Commented 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. Commented Jun 30, 2013 at 5:32

2 Answers 2

4

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 your test.py, and just replace it with main()

  • There is no need to import test1.py in your test. In doing so, this is actually running main() in your test1.py, and will thus raise an error because test.BranchName hasn't even been defined yet.

    • However, if you must import test1.py, you can actually put an if __name__ == '__main__' in there, so when you import it from test.py, it will not run.
answered Jun 30, 2013 at 5:15
Sign up to request clarification or add additional context in comments.

6 Comments

@LennartRegebro Actually, I have edited my answer regarding this
Yeah, it popped up just after I commented. Baleeteeed!
@Haidro - my goal is to print BranchName passed to test.py from test1.py,we can change the script too
@Haidro - is there a way to fix it without removing if name == 'main'
@user2341103 Afraid not. Because when you import test.py, main() will not be called as __name__ != '__main__'
|
1

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)
answered Jun 30, 2013 at 5:31

2 Comments

is there a way to get this using "if name == 'main':" in test.py in your above script..
You put that line in whatever script you want to call as a starting point. The script that is setting 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).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.