0

I have a python program that is used as a calculator. I decided to learn how to import it into other programs so I added:

if __name__ == "__main__":
    main()

Ive tried importing my module a lot of times and all it does is run the main function with just the import statement on the editor. All i do is type import calculator and it just runs it. I'm not very sure what the if name statement does so if someone would also elaborate on how it helps importing my program, that would be great.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
Hesham
  • 3
  • 3
  • 4
    Sounds like you still have something outside of the `if __name__ == '__main__'` that runs the code anyway. – user2357112 Oct 13 '17 at 18:44
  • Possible duplicate of [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – toonarmycaptain Oct 13 '17 at 18:46
  • @toonarmycaptain I don't think that's a dupe of this. This question asks that question off-hand, but this question is really asking "How come my code runs on import when I have its execution inside an `if __name__ == "__main__"` block", rather than "What does `if __name__ == "__main__"` do" – Adam Smith Oct 13 '17 at 18:50

2 Answers2

1

The if __name__ == "__main__": main() condition checks if you are running the script via python interpreter and calls the main() function. For more detailed explanation refer to this question What does if name == “main”: do?

If you have a program like this

# script.py
def hello_world():
    print "Hello World!"

hello_world()

if __name__ == "__main__":
    main()

Hello World! would be printed whether you import script.py or run it from command line like python script.py, because the function hello_world() is executed in both instances.

Case 1: Running from command line

$ python script.py
Hello World!
Traceback (most recent call last):
  File "/path/to/tests/script.py", line 8, in <module>
    main()
NameError: name 'main' is not defined

Case 2: Importing as module

>>> import script
Hello World!

If you want to stop it from being printed, then wrap the executing part of the code in the main function like this:

def hello_world():
        print "Hello World!"

def main():
    hello_world()

if __name__ == "__main__":
    main() 

Now, hello_world() gets called (or Hello World! gets printed) only when you run it as a script and not when you import it as a module.

Arunmozhi
  • 1,034
  • 8
  • 17
0

see here for more information on if __name__ == "__main__", but the quick excerpt is

Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.

So any code included in the if __name__ == "__main__" block will only be run if you call python mymodule.py. It will NOT run if you call python importsmymodule.py.

As for your specific issue, without seeing the code for the calculator module it's impossible to tell, but it seems self-evident that something in your script is calling the calculator to start.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112