#๐Ÿ”’ Someon explain me the if name == main

31 messages ยท Page 1 of 1 (latest)

whole dove
#

Someon explain me the if name == main

left domeBOT
#

@whole dove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fierce fern
#

!ifmain

left domeBOT
#
`if __name__ == '__main__'`

This is a statement that is only true if the module (your source code) it appears in is being run directly, as opposed to being imported into another module. When you run your module, the __name__ special variable is automatically set to the string '__main__'. Conversely, when you import that same module into a different one, and run that, __name__ is instead set to the filename of your module minus the .py extension.

Example

# foo.py

print('spam')

if __name__ == '__main__':
    print('eggs')

If you run the above module foo.py directly, both 'spam'and 'eggs' will be printed. Now consider this next example:

# bar.py

import foo

If you run this module named bar.py, it will execute the code in foo.py. First it will print 'spam', and then the if statement will fail, because __name__ will now be the string 'foo'.

Why would I do this?

  • Your module is a library, but also has a special case where it can be run directly
  • Your module is a library and you want to safeguard it against people running it directly (like what pip does)
  • Your module is the main program, but has unit tests and the testing framework works by importing your module, and you want to avoid having your main code run during the test
solemn pawn
#

so you can have different behaviors between running python myscript.py and python some_otherscript.py where some_otherscript.py imports myscript

solemn pawn
whole dove
#
from kivy.app import App

class Myapp(App):
    pass


if __name__ == '__main__':
    Myapp().run()

if I try in other file
I can do

Myapp().run()
#

you mean if I make a file and named it a.py
and writed

print("aaA")

when import it on other file like b.py

import a

when run b.py I will get aaA ?

#

@solemn pawn

#

but If I use ifmain I don't will get anything right?

fierce fern
#

it is also important for multiprocessing (which runs your file multiple times, doing different things in each run), so that Python knows what to run in the main process versus what to run in the helpers

whole dove
#

I didn't learn mp yet

fierce fern
#

This will print foo if you run foo.py or bar.py: ```py

foo.py

print("foo")

bar.py

import foo


This will still print if you run `foo.py`, but not if you run `bar.py`:
```py
# foo.py
if __name__ == "__main__":
    print("foo")

# bar.py
import foo
whole dove
#

I understand

fierce fern
#

I'm not sure about Kivy but sometimes when working with libraries, you may run commands like py -m kivy main.py MyApp instead of running directly py main.py, in which case Kivy will import your file and run your app in a way different from you calling MyApp().run() yourself

whole dove
#
from kivy.app import App

class Myapp(App):
    pass


if __name__ == '__main__':
    Myapp().run()

we can do Myapp().run() in other file? so we runned this? and ifmain didn't blocked that?

#

@fierce fern

#

also can you explain me the if __name__ == '__main__'

#

also what main doesn

fierce fern
fierce fern
whole dove
#

๐Ÿ’€

brittle lark
fierce fern
#

no real meaning, just what's used

a bit like why is import called import - it could be called something else, it just is what it is

whole dove
#

ok

left domeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.