2

I'm using python 2.7, the following is the simplified version of my script:

executor.py

import sys def someCal(num): num = int(num) print num*num someCal(sys.argv[1]) 

so python executor.py 13 would print out 169, it's working as expected. and I have another script, I want to make use of someCal() function in executor.py so I import it

main.py

import executor to_count = 999 executor.someCal(to_count) 

I got the error message below when execute python main.py:

 File "main.py", line 3, in <module> import executor File "/Users/mac/executor.py", line 13, in <module> someCal(sys.argv[1]) 

I don't know why it keep mentioning about line 13 in executor.py, because I didn't use that part.

Thanks in advance!

1
  • Hey,could you check an answer as correct answer please, it may help for future users who will see your question and answer. Commented Jan 11, 2015 at 22:08

2 Answers 2

3
from executor import * 

It is a better method and will work fine as you wanted.No need if name == 'main': with this method. Also you can call your functions with their names.Like:

from executor import * print (someCal(10)) 

Edit for example:

executor.py

def someCal(num): num = int(num) return num*num 

another.py

from executor import * print (someCal(10)) 

Output:

>>> 100 >>> 

If you working with functions, you should return a value in function, not print. If you return a value, you can print it later.But if you dont use return and just keep it like print num*num, then you cant use it later with print function. You can try and see that.So, returning a value is important in functions.

For your second question, check this one: What does if __name__ == "__main__": do?

Python is the best language about clear codes, so you should keep it clear, sys is not necessary for you. And you dont need if name == 'main': this statement, remember every .py file is a module, so if you can import any module without that statement,like import random; then you can import your own modules too.Just care about they have to stay in same directory so Python can find your own module/file. Keep it simple :-)

Another import method for a module is:

import executor as ChuckNorris print (ChuckNorris.someCal(10)) 

Output is same of course, you can write whatever you want instead of ChuckNorris, but be sure that name doesnt overlap with another function name in your program. For example you have a .py file called Number.py, you will import this file to another file, but you cant be sure is there any function called Number in another one, so you can call it like import Number as whatyouwanttocallit and you will avoid from that problems.

Sign up to request clarification or add additional context in comments.

1 Comment

hi @qqvc I tried it but doesn't work, still see the exact same error message
2

When you import executor in main.py, it actually doing python executor.py, I suggest you change your executor.py to:

if __name__ == '__main__': someCal(sys.argv[1]) 

and, you might want to add defensive code like if len(sys.argv)>1 before use sys.argv[1] directly

2 Comments

It seems to work! but what does if name == 'main' means?
@b937r977 "is this file the main file?"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.