#๐ Someon explain me the if name == main
31 messages ยท Page 1 of 1 (latest)
@whole dove
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.
!ifmain
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
pipdoes) - 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
it prevents the code from running when you import it as a module
so you can have different behaviors between running python myscript.py and python some_otherscript.py where some_otherscript.py imports myscript
can code example
I can run it?
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()
@solemn pawn
but If I use ifmain I don't will get anything right?
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
its my explain correct in the top?
I didn't learn mp yet
I understand
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
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
pretty much, you could do something like ```py
from main import Myapp
maybe create with custom configs
app = Myapp(configs=...)
don't even run(), just call something to test
app.test_xyz()
run, optionally with other configs
app.run(xyz=123)
it is just a special name
๐
for this example u could make it to import as a blueprint but run as an app
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
ok
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.